laravel - 如何使用 Eloquent Laravel 更新集合

标签 laravel laravel-4

我在DeviceCommand模型之间有一对多关系(每个Device有许多命令) 。现在我想使用 save() 方法更新命令集合。因此,我使用了以下代码:

$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

最佳答案

您可以在 \Illuminate\Database\Eloquent\Builder 对象上尝试 update 方法:

$queryBuilder = $device->commands()->whereStatus("pending");
$queryBuilder->update(array("status" => "sent"));

关于laravel - 如何使用 Eloquent Laravel 更新集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597266/

相关文章:

php - Mac M1 无法将 PHP 8.0 映射为 PhpStorm 中的 CLI 解释器

javascript - Laravel 5 vue.js : Property or method "comment" is not defined on the instance but referenced during render

php - 如何在错误时将类添加到输入字段

php - Sql 查询查找具有特定最后状态的订单

php - 没有主键的 Laravel 模型保存数据

mysql - SQLSTATE[23000] : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails error in Laravel 4

php - Laravel 5 实例化一个新的请求验证

php - Laravel 4:Input::has() + Input::get() vs. ($var = Input::get()) != null

php - SQLSTATE[42S22] : Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from `customer` where `` = 1 limit 1)

php - Laravel 4 中的 Eloquent 调用 : Dealing with single columns over multiple tables