@php // Ensure $products_to_loop is always iterable and each item is an object if ($product instanceof \Illuminate\Support\Collection) { $products_to_loop = $product->map(function($p){ return (object)$p; }); } elseif (is_array($product)) { $products_to_loop = collect($product)->map(function($p){ return (object)$p; }); } else { $products_to_loop = collect([$product]); } if (is_array($product) || $product instanceof \Illuminate\Support\Collection) { $row_index_start = $row_index; // original index from request foreach ($product as $key => $p) { $p->row_index = $row_index_start + $key; // assign unique row_index per variation } } else { $product->row_index = $row_index; } @endphp @foreach($products_to_loop as $product)
@endif