title: How to use chunk() in Laravel for progress and failure updates?
body: <p>Laravel Eloquent Model called <strong>ProductModel</strong> that references a table that contains a large number of rows (<em>i.e. 500,000</em>). Each row contains a <strong>product_id</strong> that calls the function <code>isValid()</code> which either continues the loop or breaks it.</p>
  <p><a href="https://laravel.com/docs/9.x/eloquent#chunking-results" rel="nofollow noreferrer">https://laravel.com/docs/9.x/eloquent#chunking-results</a></p>
  <p>The example I have created to illustrate the problem: (<em>UPDATED 16/03/22</em>)</p>
  <pre><code>$log = array();
  $log[] = ['Checking products...'];
  
  $count = 0;
  $chunks = 5000;
  $total = ProductModel::count();
  
  ProductModel::chunk($chunks, function ($products) uses (&amp;$count, $log, $total) {
      foreach ($products as $row) {
          $this-&gt;isValid($row-&gt;product_id);
          $count++;
      }
      $log[] = '[Success] Completed '. $count .' of '. $total .' records';
  });
  </code></pre>
  <p>Each record of the loop is then stored in the array <code>$log</code>.</p>
  <pre><code>[Success] Completed 5,000 of 20,000 records
  [Success] Completed 10,000 of 20,000 records
  [Success] Completed 15,000 of 20,000 records
  [Success] Completed 20,000 of 20,000 records
  </code></pre>
  <p>If an <code>Exception</code> is thrown from <code>$this-&gt;isValid()</code> the array <code>$log</code> should contain something similar to the following:</p>
  <pre><code>Checking products...
  [Success] Completed 5,000 of 20,000 records
  [Failed] Completed 7,402 of 20,000 records
  </code></pre>