Eloquent model mass update
To update multiple records in the Eloquent ORM (Object-Relational Mapping) in Laravel, you can use the update()
method on the query builder or on a model instance.
Here is an example of using the query builder to update all records in the "users" table, setting the "active" column to 1:
DB::table('users')->update(['active' => 1]);
You can also use the update()
method on a model instance to update specific records, by chaining constraints on the query:
$users = User::where('votes', '>', 100)->update(['status' => 2]);
Watch a video course
Learn object oriented PHP
You can also use the updateOrCreate
method to update or create a new record if not exist.
$user = User::updateOrCreate(
['email' => '[email protected]'],
['name' => 'John']
);
It's important to note that when using the update()
method, the updated_at column will be automatically updated with the current timestamp.