I searched everywhere for a solution for this, I hope someone already solved it or has good ideas for improvement.
I am using Laravel 5, and I came across a situation where I need to update many lines with new values. Right now I am doing a for loop for all of these rows to update them, and I would like to optimize this so as not to run many sql queries. Here is a sample code:
<?php //Well, the original code looks better than this, but the concept is the same $myrows = [0,1,2,3,4,5,6,7,8,9]; $myvalues = [45,543,657,574,234,26457,2462,897,234,89032]; for($i=0;$i<count($myrows);$i++) { MyClass::where('id', $myrows[$i])->update(['myColumn' => $myvalues[$i]]); } ?>
Obviously, this will execute 10 queries (as much as the rows I want to update), but I want to do this with only one query for optimization purposes. I know about the possibility of updating many lines simultaneously with whereIn (...) → update (...), but with this method you can update all lines to the same value, and not different ones, for example, in my example.
Thanks in advance for your help!