John Ellmore
John Ellmore

Reputation: 2229

How do I mass update an Eloquent model without a where condition?

I want to update all rows in a table with a new value for an Eloquent model. Laravel's documentation gives the following example for mass updates with Eloquent:

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

But I don't need the where condition. Ideally, I'd be able to run something like this:

App\Flight::update(['delayed' => 1]);

which omits the where clause. But this gives the error:

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

How can I mass update all the rows in my table using Eloquent? I want to avoid using the DB::table('flights')->update(...) style if possible.

Upvotes: 1

Views: 2303

Answers (1)

John Ellmore
John Ellmore

Reputation: 2229

This can be done using by running

App\Flight::query()->update(['delayed' => 1]);

The query() method creates a new query builder instance, but without any initial clauses (like where conditions). Additional operations like update() can be chained after it.

Upvotes: 6

Related Questions