There is a limit of 65,536 records.

Before:

ProductModel::whereIn('id', $productIds)
    ->each(function (ProductModel $product) {
        // @todo
    });

After:

ProductModel::whereIn('id', $productIds)
    ->chunk(100, function ($products) {
        foreach ($products as $product) {
            // @todo
        }
    });

Alternative solution by Kodeas :

$maxAtOneTime = 5000;
$total = count($productIds);

$pages = ceil($total / $maxAtOneTime);
$products = collect();

for ($i = 1; $i < ($pages + 1); $i++) {
    $offset = (($i - 1) * $maxAtOneTime);
    $start = ($offset == 0 ? 0 : ($offset + 1));

    $data = ProductModel::query()
        ->whereIn('id', $productIds)
        ->skip($start)
        ->take($maxAtOneTime)
        ->get();

    $products = $products->merge($data);
}

foreach ($products as $product) {
    // @todo
}
Zoeken