Support the ongoing development of Laravel.io →
Database Eloquent

Every night I hit my provider for an array of calls that are around 1-2k rows per night. I need to store these with eloquent but im not sure the best way to get this done. Im guessing a foreach to do $call = new calls; $call->date = $date; $call->duration = $duration; $call->moreStuff = $moreStuff;

$call->save(); for a few thousand iterations is not the best approach, any ideas?

Last updated 2 years ago.
0

1 switch KEY OFF/ON for table before and after inserting 2 use \DB::insert() instead of \Eloquent

0

Why do you need to store it uing Eloquent? Yes, it's not the best approach, it will be time and resource consuming.

The best way would be inserting in chunks using simple insert method:

foreach ($chunks as $chunk)
{
  DB::table('your_table')->insert($chunk); // single insert query
}

If you have 1k rows to insert, I suppose you can do it in one go, but for more I would do it like above.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

richbreton richbreton Joined 19 Nov 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.

© 2025 Laravel.io - All rights reserved.