尝试将数组添加到对象上具有->push
的laravel对象,但它无法正常工作。我看起来做错了什么?对象没有变化。
$cottages = Cottage::all();
foreach ($cottages as $cottage) {
//returns array
$gallery = $this->getCottageGallery($cottage['folder']);
//should append to $cottage object
$cottage->push($gallery);
}
dump($cottages);
答案 0 :(得分:0)
我实际上只是使用$cottage->setAttribute('gallery', $gallery);
将其分配给对象!
答案 1 :(得分:0)
你可以直接在集合中删除foreach
语句:
$cottages = Cottage::all()->map(function ($cottage){
$cottage->setAttribute('gallery', $this->getCottageGallery($cottage['folder']);
return $cottage;
});