@php
$sortedProducts = [];
$otherProducts = [];
// Sort products based on 'location'
foreach($products as $product) {
if (!empty($product->location) && $product->location > 0) {
$sortedProducts[$product->location] = $product;
} else {
$otherProducts[] = $product;
}
}
// Sort the products with 'location' by key
ksort($sortedProducts);
// Take the first 9 sorted products
$finalProducts = array_slice($sortedProducts, 0, 9);
$remainingSlots = 9 - count($finalProducts);
// Fill remaining slots with products that don't have a 'location'
if ($remainingSlots > 0) {
$finalProducts = array_merge($finalProducts, array_slice($otherProducts, 0, $remainingSlots));
}
// Merge any additional products for later use
$moreProducts = array_merge(array_slice($sortedProducts, 9), array_slice($otherProducts, $remainingSlots));
@endphp
{{-- Loop through final products and display in the table --}}
@foreach($finalProducts as $product)
@php
$prices = json_decode($product->price, true);
$quantity = json_decode($product->quantity, true);
@endphp