Monday, March 19, 2018

Laravel update multiple records at once - development

Laravel update multiple records at once

I have a mysql database table with 1000000 records. I wanted to update one column in each row.

I tried:

public function methodWorking()
{
    $properties = Property::with('requests')->get();
    foreach ($properties as $property) {
        $property->number_of_request = count($property->requests);
        $property->save();
    }
}

This one is working but, it’s very bad in performance wise.

I wanted to write a code like this:

public function methodExpect()
{
    $properties = Property::with('requests')->get();
    $property_array = [];
    foreach ($properties as $property) {
        $property->number_of_request = count($property->requests);
        $property_array[] = $property;
    }

    Property::save($property_array);
}

Is it possible with laravel ?

Thanks.



from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-update-multiple-records-at-once/
via Lzo Media

No comments:

Post a Comment