@foreach ($categories as $category)
@php
$countProduct = 0;
if (
$category->sub_categories &&
$category->sub_categories->isNotEmpty()
) {
foreach ($category->sub_categories as $subCat) {
if ($category->cat_id == 26) {
// dd($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();
// dd( $countProduct);
if (
$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() == 0
) {
$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();
}
}
} else {
$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();
}
@endphp
{{-- @dd($categories) --}}
@if ($countProduct > 0)
{{-- @foreach ($category->sub_categories as $subCat) --}}
@foreach ($categories as $category)
@php
$countProduct = 0;
if (
$category->sub_categories &&
$category->sub_categories->isNotEmpty()
) {
foreach ($category->sub_categories as $subCat) {
if ($category->cat_id == 26) {
// dd($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();
// dd( $countProduct);
if (
$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() == 0
) {
$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();
}
}
} else {
$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();
}
@endphp
{{-- @dd($categories) --}}
@if ($countProduct > 0)
{{-- @foreach ($category->sub_categories as $subCat) --}}