title: Laravel insert data run out of memory
body: <p>Using Laravel 4.2, I am using an Artisan command that reads data from a fixed length text files and inserts into our database.  I need to insert this data into our database using a connection that is not the default.  When I do this, I run out of memory while parsing the data. 'Allowed memory size of 1073741824 bytes exhausted'.  <strong>Why do I run out of memory using the non-default database connection, but when I use the 'default' database
  connection it works?</strong></p>
  
  <p>If I use the 'default' connection, I can load the data with no errors.  Works perfect except the data is in the wrong database.</p>
  
  <p>If I use the 'static' connection (connection that is not the default), I run out of memory every time.</p>
  
  <p><strong>Thinks I've tried with no success:</strong></p>
  
  <p>I am disabling query logging.</p>
  
  <pre><code>\DB::disableQueryLog();
  </code></pre>
  
  <p>Change the database default connection.</p>
  
  <pre><code>\Config::set('database.default', \Config::get('database.static'));
  </code></pre>
  
  <p>Use Model as Eloquent Model with DB insert</p>
  
  <pre><code>$model = new \vendor\VendorCat();
  \DB::connection(\Config::get('database.static'))
     -&gt;table('vendorcat')
     -&gt;insert($model-&gt;toArray());
  </code></pre>
  
  <p>Use Model as stdClass</p>
  
  <pre><code>$model = new \stdClass();
  \DB::connection(\Config::get('database.static'))
      -&gt;table('vendorcat')
      -&gt;insert(json_decode(json_encode($model), TRUE));
  </code></pre>
  
  <p>Database Config:</p>
  
  <pre><code>return array(
   'log' =&gt; FALSE,
   'fetch' =&gt; PDO::FETCH_CLASS,
   'default' =&gt; 'mysql',
   'static' =&gt; 'defaultstatic',
   'connections' =&gt; array(
   'mysql' =&gt; array(
    'driver' =&gt; 'mysql',
    'host' =&gt; '127.0.0.1',
    'database' =&gt; 'PRODUCTION',
    'username' =&gt; 'username',
    'password' =&gt; 'password',
    'charset' =&gt; 'utf8',
    'collation' =&gt; 'utf8_unicode_ci',
    'prefix' =&gt; '',
    ),
   'defaultstatic' =&gt; array(
    'driver' =&gt; 'mysql',
    'host' =&gt; '127.0.0.1',
    'database' =&gt; 'PRODUCTIONSTATIC',
    'username' =&gt; 'username',
    'password' =&gt; 'password',
    'charset' =&gt; 'utf8',
    'collation' =&gt; 'utf8_unicode_ci',
    'prefix' =&gt; '',
    ),
   ),
   'migrations' =&gt; 'migrations',
   'redis' =&gt; array(
    'cluster' =&gt; FALSE,
    'default' =&gt; array(
    'host' =&gt; '127.0.0.1',
    'port' =&gt; 6379,
    'database' =&gt; 0,
    ),
   ),
  );
  </code></pre>
  
  <p>Code used to process data:</p>
  
  <pre><code>\DB::disableQueryLog();
  $list = scandir($path, SCANDIR_SORT_ASCENDING);
  foreach($list as &amp;$file) {
    $fullpath = $path . '/' . $file;
    $pi = pathinfo($fullpath);
    if ( strcasecmp('dat', $pi['extension']) != 0 ) {
      continue;
    }
    $cnt++;
    $this-&gt;processFile($fullpath);
    $fullpath = null;
    unset($fullpath);
    $pi = null;
    unset($pi);
  }
  $list = null;
  unset($list);
  
  
  
  while ( ! feof($hdl) ) {
    $line = fread($hdl, $len);  // fixed length text 1503 bytes
    if ($line === FALSE) break;
    if (strlen($line) == 0) break;
    $model = new \vendor\VendorCat();
    //$model = new stdClass();
    /** set model properties, 86 properties, example... */
    $model-&gt;engprop = trim(substr($line, 1174, 1));
    $model-&gt;engho = trim(substr($line, 1176, 1));
  
    $model-&gt;save();
  
    // $model-&gt;setConnection(\Config::get('database.static'))-&gt;save();
  
    // $model = new \vendor\VendorCat();
    // \DB::connection(\Config::get('database.static'))
    //   -&gt;table('vendorcat')
    //   -&gt;insert($model-&gt;toArray());
  
    // $model = new \stdClass();
    // \DB::connection(\Config::get('database.static'))
    //   -&gt;table('vendorcat')
    //   -&gt;insert(json_decode(json_encode($model), TRUE));
  
    $line = null;
    unset($line);
    $model = null;
    unset($model);
  }
  </code></pre>