Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bran921007/f107148e9e1bf999d847d2802512ff35 to your computer and use it in GitHub Desktop.
Save bran921007/f107148e9e1bf999d847d2802512ff35 to your computer and use it in GitHub Desktop.
Update multiples users with one Query, instead of foreach of collection
// Makes one query per user
// update `users` set `subscription` = 'lifetime' where id = ?;
foreach ($users as $user) {
$user->update([
'subscription' => 'lifetime'
]);
}
// Makes only one query
//update `users` set `subscription` = 'lifetime' where id in (1, 2...);
$users->toQuery()->update([
'subscription' => 'lifetime',
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment