where('status', 1) ->orderBy('sort_order') ->orderBy('id') ->get(); return $this->buildTree($items); } /** * Recursively build the menu tree from a flat collection. * * Only enabled (status = 1) items are kept at every level, so a disabled * item is excluded whether it is a root node or a nested child. */ protected function buildTree($items, ?int $parentId = null): array { $tree = []; foreach ($items as $item) { // Exclude disabled items at every level. if (! $item->status) { continue; } $itemParentId = $item->parent_id === null ? null : (int) $item->parent_id; if ($itemParentId === $parentId) { $children = $this->buildTree($items, (int) $item->id); $item->setRelation('children', collect($children)); $tree[] = $item; } } return $tree; } }