@@ -160,30 +160,15 @@ protected function setUp(): void
160160
161161 protected function buildTree (): Collection
162162 {
163- // If we have a modifyQueryUsing callback, use a single query approach
164- // This handles filtered queries that might not fit the standard null/non-null parent structure
165- if ($ this ->modifyQueryUsing ) {
166- $ query = $ this ->getQuery ()->clone ();
167- $ query = $ this ->evaluate ($ this ->modifyQueryUsing , ['query ' => $ query ]);
168-
169- if ($ this ->withTrashed ) {
170- $ query ->withTrashed ($ this ->withTrashed );
171- }
172-
173- $ results = $ query ->get ();
174-
175- // Store results for additional functionality
176- if ($ this ->storeResults ) {
177- $ this ->results = $ results ;
178- }
179-
180- return $ this ->buildTreeFromResults ($ results );
181- }
182-
183- // Original logic for non-filtered queries
163+ // Start with two separate query builders
184164 $ nullParentQuery = $ this ->getQuery ()->clone ()->where ($ this ->getParentAttribute (), $ this ->getParentNullValue ());
185165 $ nonNullParentQuery = $ this ->getQuery ()->clone ()->whereNot ($ this ->getParentAttribute (), $ this ->getParentNullValue ());
186166
167+ // If we're not at the root level and a modification callback is provided, apply it to null query
168+ if ($ this ->modifyQueryUsing ) {
169+ $ nullParentQuery = $ this ->evaluate ($ this ->modifyQueryUsing , ['query ' => $ nullParentQuery ]);
170+ }
171+
187172 // If we're at the child level and a modification callback is provided, apply it to non null query
188173 if ($ this ->modifyChildQueryUsing ) {
189174 $ nonNullParentQuery = $ this ->evaluate ($ this ->modifyChildQueryUsing , ['query ' => $ nonNullParentQuery ]);
@@ -238,23 +223,10 @@ private function buildTreeFromResults($results, $parent = null): Collection
238223
239224 // Recursively build the tree starting from the root (null parent)
240225 $ rootResults = $ resultMap [$ parent ] ?? [];
241-
242- // If we have no root results but we have results, and we're using modifyQueryUsing,
243- // it means the query was filtered and might not have proper root items.
244- // In this case, show all results as root items to prevent empty trees.
245- if (empty ($ rootResults ) && ! empty ($ results ) && $ this ->modifyQueryUsing ) {
246- foreach ($ results as $ result ) {
247- // Build a node and add it to the tree
248- $ node = $ this ->buildNode ($ result , $ resultMap , $ disabledOptions , $ hiddenOptions );
249- $ tree ->push ($ node );
250- }
251- } else {
252- // Normal tree building - only show items that are actually root items
253- foreach ($ rootResults as $ result ) {
254- // Build a node and add it to the tree
255- $ node = $ this ->buildNode ($ result , $ resultMap , $ disabledOptions , $ hiddenOptions );
256- $ tree ->push ($ node );
257- }
226+ foreach ($ rootResults as $ result ) {
227+ // Build a node and add it to the tree
228+ $ node = $ this ->buildNode ($ result , $ resultMap , $ disabledOptions , $ hiddenOptions );
229+ $ tree ->push ($ node );
258230 }
259231
260232 return $ tree ;
0 commit comments