@php $hasChildren = $category->children->where('is_active', true)->count() > 0; $isFolder = $category->isFolder() || $hasChildren; $totalDocs = $category->activeDocuments->count(); // Calculate total documents including children if ($hasChildren) { foreach ($category->children->where('is_active', true) as $child) { $totalDocs += $child->activeDocuments->count(); foreach ($child->children->where('is_active', true) as $grandchild) { $totalDocs += $grandchild->activeDocuments->count(); } } } @endphp