@foreach ($categories as $category) @php // Calculate total product count including all children $totalCount = $category->products->count(); if ($category->children->count()) { foreach ($category->children as $child) { $totalCount += $child->totalProducts(); } } @endphp
  • {{ $category->name }} ({{ $totalCount }})
  • {{-- Recursive child rendering --}} @if ($category->children->count()) @include('pages.product.category-tree', [ 'categories' => $category->children, 'level' => $level + 1 ]) @endif @endforeach