Ad

Laravel 5.2 - Updating Multiple Rows With One Query

- 1 answer

So I do a lot of calculations and at the end I have rates that need to be saved to existing rows in a table.

The array I have will be similar to the following:

[
    <model_id> => [
        'rate' => <some rate>
    ]
    <model_id_2> => [
        'rate' => <some other rate>
    ]
    .....
]

Now obviously I could foreach through this array and do an update for each and every item in the array but I could end up with 100 update calls. Is there a way (through laravel's eloquent OR even a raw sql query) to do all these updates through one call?

Ad

Answer

If you are worried about the request spent time you can handle this by firing an event and then queueing your listener/job, who will save your model, so it can be processed asynchronously. For examples, go to Laravel Docs for Queues

As long as I know you cannot update multiple rows on Laravel.

Ad
source: stackoverflow.com
Ad