给定Collection
Eloquent Models
,它们是Arrayable
,如何获取这些对象的数组?获取laravel集合中的模型数组
如果我在集合上调用->toArray()
,它会给我一个嵌套的关联数组,从而破坏模型。
如果我将其转换为一个数组,我得到这个非常奇怪的事情:
array:1 [▼
"\x00*\x00items" => array:1 [▼
"temp" => HistorySeries {#374 ▼
#table: "history_series_hse"
#primaryKey: "id_hse"
#connection: "mysql"
+timestamps: false
<...snip...>
}
]
]
再有就是这一点,但我真的不喜欢它(它的工作原理):
$reflection = new ReflectionClass($coll);
$property = $reflection->getProperty('items');
$property->setAccessible(true);
$array = $property->getValue($coll);
或者我可以使用foreach循环提取它,但这很丑陋。任何不错的方式?
试试'$ collection-> all()'? – ceejayoz
您是否尝试过[每个()](https://laravel.com/docs/5.2/collections#method-each)方法? –