【发布时间】:2019-02-05 19:08:05
【问题描述】:
我在 Laravel 中使用以下方法创建了一个集合:
use Illuminate\Database\Eloquent\Collection; // As per Mihir Bhende's answer, make sure we're using the correct Eloquent `Collection`
$collection = new Collection;
然后我继续将多个项目(所有相同的模型类型)推送到该集合中:
$item = $items->first(function($item) {
return $item->field == "value";
});
$collection->push($item);
最后,我尝试使用$collection->update() 或$collection->save() 更新$collection 中的所有记录,但我收到以下错误:
“方法 Illuminate\Support\Collection::update 不存在。”
但是由于我使用的是new Collection(我也尝试过collect()),并且我非静态地调用update(),所以我没想到会出现这个错误。
如何一次更新$collection 中的数据库记录? 或者,有没有一种方法可以将多个不同子对象的更改“排队”而不牺牲性能? p>
谢谢!
【问题讨论】: