Reputation: 1668
I am trying to update all records from table withing single eloquent query in Laravel 5.4
Currency::update(['default'=>0]);
I am expecting that this query will update all the records from a table, but this query isn't working. It is expecting where clause and I don't want to put any conditions using where clause, I just want to update all records in a table.
I am not getting any answer from laravel documentation.
Upvotes: 9
Views: 10496
Reputation: 61
Product::query()->update(['default' => 0, ]);
Simple query to update all rows of the table.
Upvotes: 6
Reputation: 1293
If your MySQL server running SQL_SAFE_UPDATES mode you can use "where" like this. Or another "where clause" that includes all results.
Currency::where('id', '>', 0)->update(['default' => 0]);
Upvotes: 3
Reputation: 522752
MySQL has a safe update server mode which will not allow an update to happen unless the statement has a WHERE
clause involving the primary key. If your MySQL be running in this mode, then one way to spoof it would be to use WHERE id = id
, something like this:
DB::table('Currency')
->where('id', 'id')
->update(['default' => 0]);
Upvotes: 8