Laravel batch update a collection of eloquent models.
Perform single query for batch update collection of models.
It will also update updated_at
column of the models
composer require waelmoh/laravel-update-insert-many
Update collection of models. This perform a single update query for all the passed models. Accepts an array of arrays or collection. Each array|object should contain the model key and the data to update. The key can be any column, by default it is id
. Also key can be an array of columns.
Only the dirty or changed attributes will be included in the update.
This updates the updated_at
column of the models and the tables.
$users = [
[
'id' => 31,
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '[email protected]'
],
[
'id' => 32,
'first_name' => 'Hubert',
'last_name' => 'Wiza',
'email' => '[email protected]'
],
[
'id' => 33,
'first_name' => 'Mikayla',
'last_name' => 'Keeling',
'email' => '[email protected]'
]
]
User::updateMany($users); // update many models using id as the default key
User::updateMany($users, 'id'); // same as above
User::updateMany($users, 'username'); // use username as key instead of id
User::updateMany($users, ['username', 'email']); // use username and email as keys instead of id
User::updateMany($users, ['username', 'email'], ['last_name']); // update last name if username and email match
User::updateMany($users, 'id', ['email', 'first_name', 'last_name']);
This will produce a query like this:
UPDATE
`users`
SET
`email` =
CASE
WHEN
`id` = '31'
THEN
'[email protected]'
WHEN
`id` = '32'
THEN
'[email protected]'
WHEN
`id` = '33'
THEN
'[email protected]'
ELSE
`email`
END
, `first_name` =
CASE
WHEN
`id` = '31'
THEN
'John'
WHEN
`id` = '32'
THEN
'Hubert'
WHEN
`id` = '33'
THEN
'Mikayla'
ELSE
`first_name`
END
, `last_name` =
CASE
WHEN
`id` = '31'
THEN
'Doe'
WHEN
`id` = '32'
THEN
'Wiza'
WHEN
`id` = '33'
THEN
'Keeling'
ELSE
`last_name`
END
WHERE
`id` IN
(
31, 32, 33
);