0

Laravel Eloquent Model called ProductModel that references a table that contains a large number of rows (i.e. 500,000). Each row contains a product_id that calls the function isValid() which either continues the loop or breaks it.

https://laravel.com/docs/9.x/eloquent#chunking-results

The example I have created to illustrate the problem: (UPDATED 16/03/22)

$log = array();
$log[] = ['Checking products...'];

$count = 0;
$chunks = 5000;
$total = ProductModel::count();

ProductModel::chunk($chunks, function ($products) uses (&$count, $log, $total) {
    foreach ($products as $row) {
        $this->isValid($row->product_id);
        $count++;
    }
    $log[] = '[Success] Completed '. $count .' of '. $total .' records';
});

Each record of the loop is then stored in the array $log.

[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

If an Exception is thrown from $this->isValid() the array $log should contain something similar to the following:

Checking products...
[Success] Completed 5,000 of 20,000 records
[Failed] Completed 7,402 of 20,000 records
Ryan
  • 1
  • 5
  • Declare `$count = 0` outside the loop and then as you say you need to do `use (&$count, $log)` (not sure if you need to pass the log by reference since you are only appending to it rather than changing its whole value) – apokryfos Mar 03 '22 at 06:39
  • What exactly do you need in `$log` when `$this->isValid()` throws? – Tez Mar 15 '22 at 08:19

0 Answers0