Error entering Laravel DB: allowed memory size is exhausted

I have a problem trying to insert ~ 20K records into my database. I notice that although I am repeating inside my foreach loop, I am not getting anything output on the command line. Instead, I get the error Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 91 bytes) in /Users/me/Sites/Laravel/database/connection.php on line 293 after inserting ~ 9440 records.

Here is my code (tried using both Eloquent and Fluent):

 <?php class Process_Controller extends Base_Controller { public function action_migrate() { $properties = DB::table('raw_properties')->get('id'); $total = count($properties); foreach($properties as $x => $p){ $r = RawProperty::find($p->id); $count = $x +1; $prop_details = array( 'column' => $r->field, // Total of 21 fields ); DB::table('properties')->insert($prop_details); echo "Created #$count of $total\n"; } } } 
+5
source share
2 answers

This error indicates that your PHP script has run out of memory limits due to insufficient memory allocated for the script.

You need to increase memory_limit using the ini_set function, for example, ini_set ('memory_limit', '128M');

-one
source

The accepted answer captures the symptom, not the problem. The problem is that the Laravel query log (in memory) consumes all of your RAM when doing so many queries. See the answer here: stack overflow

Or, in short, turn off query logging via:

 DB::disableQueryLog() 

Before executing 20k queries

+30
source

All Articles