title: Laravel 8 - traverse very large database table and update record
body: <p>I have got a Laravel 8 app. I would like to update a large database table from within my application. I added two new columns to the table and now I would like to update their content with the information of other columns per record.</p>
  <p>My table has about 5.000.000 entries that need to be updated.</p>
  <p>My problem is that the script being run from the browser either hits the memory exhaustion or the maximum execution time.</p>
  <p>The controller function is as follows. I have set the retrieved number of rows to be limited to not run into the issues of time and memory.</p>
  <pre><code>public function fillDB()
  {
      $entries = TABLE_MODEL::query()
                  -&gt;orWhereNull(&quot;number_correct&quot;)
                  -&gt;orWhereNull(&quot;number_wrong&quot;)
                  -&gt;limit(5000)
                  -&gt;get();
  
      foreach ($entries as $item) {
          $item-&gt;setNumberCorrect($item-&gt;getNumberCorrectWrong(true));
          $item-&gt;setNumberWrong($item-&gt;getNumberCorrectWrong(false));
          $item-&gt;save();
      }
  }
  </code></pre>
  <p>I update the columns <code>number_correct</code> and <code>number_wrong</code> on each record where either one of these columns is still <code>NULL</code>. I cannot use a MySQL query to update the new columns, because I need to evaluate some information in each record to figure out the right values.</p>
  <p>Is there another way of updating the table so I can run the update process at once?</p>
  <p>Thank you very much in advance.</p>