F
F
FANTASANTA2022-03-29 13:51:22
MySQL
FANTASANTA, 2022-03-29 13:51:22

Proper MySQL Bulk Update in Laravel?

There are about 10,000 products, you need to update prices by category, for each group of products by category there are certain price increase rates. But is it correct to do the update inside the loop?

There is only one solution that comes to mind right now:

// И так более 10 тысяч запросов за раз
foreach($products as $p) {
Products::where('id', $p->id)->update(['price' => $newPrice]);
}


Is it correct to do a mass update this way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2022-03-29
@FANTASANTA

If there are no event hooks associated with these fields, then it's better to do everything through bulk operations (and yes, arbitrary expressions can also be used there )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question