Created
April 7, 2022 01:36
-
-
Save bran921007/f107148e9e1bf999d847d2802512ff35 to your computer and use it in GitHub Desktop.
Update multiples users with one Query, instead of foreach of collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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