我在Device
和Command
模型之间建立了一对多的关系(每个Device
有多个commands
)。现在我想使用save()
方法更新一组commands
。所以,我使用了以下代码:
$device = Device::find(1);
$commands = $device->commands()->whereStatus("pending")->get();
$commands->status = "sent";
$commands->save();
但我得到了一个 FatalErrorException
异常,错误消息为 Call to undefined method Illuminate\Database\Eloquent\Collection::save()
。
换句话说,我正在寻找在 Eloquent
中与以下 MySQL 查询等价的查询:
UPDATE commands SET status = 'sent' WHERE status = 'pending';
使用 Laravel 4.2
$collection
实际上不是一个集合,而是查询构建器类的一个实例,update()方法并不属于Collection类。 - cdarken$collection
是 QueryBuilder 的实例。 - eithed