I was learning about How to insert multiple rows from a single query using eloquent/fluent and I found the answer here
Can somebody share any documentation about how to update bulk rows in single query?
My queries are below.
Update tblrole set role = 'Super Admin' where RoleID = 1;
Update tblrole set role = 'Super Admin A' where RoleID = 2;
Update tblrole set role = 'Super Admin B' where RoleID = 3;
Update tblrole set role = 'Super Admin C' where RoleID = 4;
You might find inspiration from this mass insert or update gist:
}
Ref: https://gist.github.com/RuGa/5354e44883c7651fd15c
I don't think I need to provide any explanation as each chunk of code in the function speaks of itself.
You cannot do anything like this in simple way. You can easily update multiple rows with same value but if you want to update
role
column with different values it will be tricky.In fact it doesn't make much sense to do it like this but if you really want it and you think it's the best solution you can try to play with raw queries using technique described here https://stackoverflow.com/a/25674827/3593996
You can solve the issue using a single MySQL query. It can be implemented in Laravel Eloquent using DB::raw() method.