Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danimohamadnejad's avatar

how to update all rows using one query?

Hello. please consider following data: $data=[ ['id'=>'1','stock'=>'12'], ['id'=>'2','stock'=>'13'] ]; how can I update all stocks using IDs using one query?

0 likes
5 replies
Sinnbeck's avatar

I don't believe this to be possible (or rather it might be, but your query would have to be filled with a load of CASE statements)

munazzil's avatar

Without Code ...... Just check like below,

   public function update(Request $request)
                              {
                                 $data = $request->all();
                                 update($data);
                                }
sairum's avatar
Stock::where('id',1)->get()->each( function($stock){

$stock-stock = 12;
});

change everything according to your model this way you can update with eloquent query

Sinnbeck's avatar

Yes. It will run for an update query for each item :)

Please or to participate in this conversation.