title: How to chunk results from a custom query in Laravel
body: <p>I have a custom query that grabs data from the old system and maps it to models in the new system. The query looks like this:</p>
  
  <p><code>$companies = DB::connection('legacy')-&gt;select("...");</code></p>
  
  <p>And since it's a lot of data, I'd like to use Eloquent's chunk feature (just sample code copied from their docs):</p>
  
  <pre><code>User::chunk(200, function($users)
  {
      foreach ($users as $user)
      {
          //
      }
  });
  </code></pre>
  
  <p>How do I implement this?</p>
  
  <hr>
  
  <p>Edit: My code now looks like this, which results in no response:</p>
  
  <pre><code>DB::connection('legacy')-&gt;select("SELECT * FROM companies")-&gt;chunk(200, function($companies) {
      foreach ($companies as $company) {
          // dd($company);
          $entity       = Entity::firstOrNew(['external_id' =&gt; $company-&gt;companyKey]);
          $entity-&gt;name = $company-&gt;companyName;
          $entity-&gt;save();
      }
  });
  </code></pre>