Skip to content

Commit 2fcfd8b

Browse files
committed
Fix tree bug
1 parent 26efb68 commit 2fcfd8b

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/Components/Menu.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,32 @@ protected function getMenuItems($menu, ?string $locale = null)
6363
return collect();
6464
}
6565

66+
$maxDepth = $menu->getEffectiveMaxDepth();
67+
$eagerLoadRelations = $this->buildEagerLoadRelations($maxDepth);
68+
69+
// Get only top-level menu items with their visible children based on max depth
6670
return $menu->menuItems()
67-
->with('linkable', 'children')
71+
->with($eagerLoadRelations)
6872
->visible()
6973
->ordered()
70-
->get()
71-
->toTree();
74+
->get();
7275
}
76+
77+
protected function buildEagerLoadRelations(int $maxDepth): array
78+
{
79+
$relations = ['linkable'];
80+
$currentPath = '';
81+
$depth = 1;
82+
83+
while ($depth < $maxDepth) {
84+
$currentPath .= $depth === 1 ? 'children' : '.children';
85+
$relations[$currentPath] = function ($query) {
86+
$query->visible()->ordered()->with('linkable');
87+
};
88+
$depth++;
89+
}
90+
91+
return $relations;
92+
}
93+
7394
}

0 commit comments

Comments
 (0)