Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

That depends on how you are passing the new values through initially.

If you're posting to a route with the new values, you could set up a custom method to do something like

public function multiUpdate()
{
    $newValues = Input::get('values');

    foreach ($newValues as $key => $value) {
        $tableData = Model::findOrFail($key);

        $tableData['column'] = $value;

        if (!$setting->save()) return Redirect::back()->withInput()->withErrors($setting->errors());
     
    }

    return Redirect::back()->with('Success', "Table was successfully updated!");
}
Last updated 2 years ago.
0

Yeah, thats horrible, because you are running a query for each update. 1000 users? 1000 queries. Not good.

Last updated 2 years ago.
0

This is a terrible solution and I'm still trying to find something better. I have to update ~10k rows and doing them like this is SLOW :(

Last updated 2 years ago.
0

This is from the official docs:

$affectedRows = User::where('votes', '>', 100)->update(array('status' => 2));

Sorry, didnt really read the question. This will not produce the CASE WHEN you're asking for. But it's still better than going through all records (inlcuding a select)

Last updated 2 years ago.
0

http://stackoverflow.com/questions/34885575/laravel-5-2-updating-multiple-rows-with-one-query?noredirect=1#comment57509405_34885575

I have actually asked this exact question. If anyone has any idea how to do this it would be greatly appreciated.

0

why not just do a raw query? i know it's not the eloquent way but in your case this will give you better performance.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

tanshiqi tanshiqi Joined 15 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.