@foreach ($stocks as $productId => $stockGroup) @php // Find the first stock with qty > 0 $availableStock = $stockGroup->first(function ($stock) { return $stock->qty > 0; }); // Aggregate total quantity for the group $totalQty = $stockGroup->sum('qty'); // Use the first available stock's product or fallback to the first stock's product $product = $availableStock ? $availableStock->product : $stockGroup->first()->product; // Determine unit price from the first available stock or fallback $unitPrice = $availableStock ? $availableStock->unit_price : 0; $stockId = $availableStock ? $availableStock->id : $stockGroup->first()->id; @endphp @if (!empty($product)) @endif @endforeach