如何在没有 where 条件的情况下批量更新 Eloquent 模型?

Joh*_*ore 1 php laravel eloquent

我想用 Eloquent 模型的新值更新表中的所有行。Laravel 的文档给出了以下使用 Eloquent 进行大规模更新的示例:

App\Flight::where('active', 1)
          ->where('destination', 'San Diego')
          ->update(['delayed' => 1]);
Run Code Online (Sandbox Code Playgroud)

但我不需要 where 条件。理想情况下,我将能够运行这样的事情:

App\Flight::update(['delayed' => 1]);
Run Code Online (Sandbox Code Playgroud)

其中省略了where子句。但这给出了错误:

PHP Deprecated:  Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically on line 1
Run Code Online (Sandbox Code Playgroud)

如何使用 Eloquent 批量更新表中的所有行?DB::table('flights')->update(...)如果可能,我想避免使用该样式。

Joh*_*ore 6

这可以通过运行来完成

App\Flight::query()->update(['delayed' => 1]);
Run Code Online (Sandbox Code Playgroud)

query()方法创建一个新的查询构建器实例,但没有任何初始子句(如where条件)。update()可以在它之后链接其他操作。