Vishal Shetty
Vishal Shetty

Reputation: 1668

Laravel 5.4 update all records in table without using where clause

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.

Table Structure enter image description here

Upvotes: 9

Views: 10496

Answers (3)

Aftab Khaliq
Aftab Khaliq

Reputation: 61

Product::query()->update(['default' => 0, ]);

Simple query to update all rows of the table.

Upvotes: 6

Okan
Okan

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

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions