Update query in bulk using single query instead of using loop
Hello all,
I'm looking for a query to update in bulk using single query instead of using query in loop. I have more than 2000 products and I have to update there stock value. Right now I using loop for each product so its take too much time. Here is my code
A way to do this is to collect all (product_id - transfer_id)'s as a concatenated strings and then make a ->update query to all TransferSourceProducts whereIn -> you create a virtual/calculated column of the concatenated product_id , transfer_id fields (via DB:raw and Concat(product_id, '-', transfer_id)).
This is if you use eloquent but better if you do it in just a Native Query (it's much simpler).
This is achievable only if you create a stored procedure in your SQL server and call it from your PHP providing the prepared structure of data (product - transfer - onhand_value). That way you can update the SQL rows with only one call to your SQL server.