title: How do I use Laravel's chunk to avoid running out of memory?
body: <p>I am pulling around 100k records from temporary tables, doing some slight modification on the data, downloading photos, and then putting the fields I need to keep in a "master" table.  This is quickly causing my app to crash as it runs out of memory.</p>
  
  <p>I read the very brief docs on using chunk() with Laravel's eloquent ORM but do not know how to even begin to implement it in my class.</p>
  
  <p>Here is what I am currently doing:</p>
  
  <pre><code>    public function fire()
  {
      // Turn off query logging
      DB::connection()-&gt;disableQueryLog();
  
      $feeds = RetsFeed::where('active','=',1)-&gt;get();
      foreach ($feeds as $feed)
      {
  
          $class = "TempListing{$feed-&gt;board}";
  
          $listings = $class::orderBy('MatrixModifiedDT','desc')-&gt;get();
  
          $listings-&gt;each(function($listing) use ($feed) {
              ListingMigrator::migrateListing($listing,$feed);
              echo "Feed: $feed-&gt;board\r\n";
              echo "SubcondoName: $listing-&gt;SubCondoName\r\n";
              echo "Development: $listing-&gt;Development\r\n";
              echo "\r\n";
          });
      }
  
  }
  </code></pre>
  
  <p>Each feed (or datasource) is dumped into a temp table in a different chore.  That works fine.  Then, I grab all of hte listings out of one table (which is around 30k on average) and run my ListingMigrator method.</p>
  
  <p>Where do I put the chunk in this example?  Would it replace the line:</p>
  
  <pre><code>$listings = $class::orderBy('MatrixModifiedDT','desc')-&gt;get();
  </code></pre>
  
  <p>I don't fully understand the closure in the eloquent docs.  This is all they have to say about it and here's the code example from the Laravel site:</p>
  
  <pre><code>    User::chunk(200, function($users)
  {
      foreach ($users as $user)
      {
          //
      }
  });
  </code></pre>