@foreach ($categories as $category)
@php
$countProduct = 0;
// Count products directly in the main category
$countProduct += $category
->products()
->whereHas('variations', function ($query) {
$query->where('is_deleted', 0); // or whatever your availability field is
// OR if you check stock quantity:
// $query->where('stock', '>', 0);
})
->where('is_deleted', 0)
->count();
// Count products in subcategories
foreach ($category->sub_categories as $subCat) {
$countProduct += $subCat
->products()
->whereHas('variations', function ($query) {
$query->where('is_deleted', 0); // or whatever your availability field is
// OR if you check stock quantity:
// $query->where('stock', '>', 0);
})
->where('is_deleted', 0)
->count();
}
@endphp
@if ($countProduct > 0)
{{ $countProduct }} items
@endif
@endforeach