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