How to use Laravel chunk to avoid running out of memory?

I collect about 100 thousand records from temporary tables, do a small modification of the data, upload photos, and then put the fields that I need to store in the "main" table. This quickly crashes my application as it runs out of memory.

I read very brief docs about using chunk () with the eloquent ORM Laravel, but don't know how to even start implementing it in my class.

Here is what I am doing now:

public function fire() { // Turn off query logging DB::connection()->disableQueryLog(); $feeds = RetsFeed::where('active','=',1)->get(); foreach ($feeds as $feed) { $class = "TempListing{$feed->board}"; $listings = $class::orderBy('MatrixModifiedDT','desc')->get(); $listings->each(function($listing) use ($feed) { ListingMigrator::migrateListing($listing,$feed); echo "Feed: $feed->board\r\n"; echo "SubcondoName: $listing->SubCondoName\r\n"; echo "Development: $listing->Development\r\n"; echo "\r\n"; }); } } 

Each feed (or data source) is dumped into a temporary table in another job. It works great. Then I grab all the hte lists from one table (on average about 30k) and run my ListMigrator method.

Where do I put the piece in this example? Will it replace the line:

 $listings = $class::orderBy('MatrixModifiedDT','desc')->get(); 

I do not fully understand the closure in eloquent documents. That's all they have to say about it, and here is a sample code from the Laravel website:

  User::chunk(200, function($users) { foreach ($users as $user) { // } }); 
+7
php mysql eloquent laravel laravel-4
source share
1 answer

The chunk call should replace the get call - it is designed to work with the pre-get () request constructor object.

 $listings = $class::orderBy('MatrixModifiedDT','desc'); $listings->chunk(200, function($listings) use($feed) { $listings->each(function($listing) use ($feed) { ListingMigrator::migrateListing($listing,$feed); echo "Feed: $feed->board\r\n"; echo "SubcondoName: $listing->SubCondoName\r\n"; echo "Development: $listing->Development\r\n"; echo "\r\n"; }); }); 
+14
source share

All Articles