Support the ongoing development of Laravel.io →
Eloquent Laravel

I have a relatively large number of records that will need to be updated once a minute (anywhere from 3,000 to probably 12,000, usually somewhere in the 6,000 range). Currently I am just using a for loop like so:

$some_id = AnotherModel::where("label", "=", $label)->first()->id;
foreach ($Data->results as $result) {
    Model::updateOrCreate(
        ["symbol" => $result->symbol, "some_id" => $some_id],
        [
            "name" => $result->name

 		]
 	)
}

this process on my local takes about 49 seconds (thats including the API call (just one) I make to a separate service which does take a while since its so much data, so the DB updating may only be 35 seconds or so), which would normally be fine but I have to do some more mass updating after this as well so I'm a bit afraid I am going to be running short on time, is there a more efficient way to do these updateOrCreate calls other than just a for loop? If there isn't then I may just have to report we may need to push the update frequency back to 3 or 5 min...

Last updated 3 years ago.
0

You can try this code:

public function updateOrCreate(array $attributes, array $values = []) { return tap($this->firstOrNew($attributes), function ($instance) use ($values) { $instance->fill($values)->save(); }); }

By:Xtreem Solution

Highly Skilled Laravel Developer

Dedicated PHP Developer

0

Sign in to participate in this thread!

Eventy

Your banner here too?

djarrin djarrin Joined 27 Sep 2016

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.

© 2025 Laravel.io - All rights reserved.