Skip to content

Commit b953e6d

Browse files
Merge pull request #178 from Baspa/4.x
fix: SelectTree shows empty dropdown when filtered results have no root items
2 parents 5451de4 + 7e57336 commit b953e6d

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/SelectTree.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,29 @@ protected function setUp(): void
160160

161161
protected function buildTree(): Collection
162162
{
163-
// Start with two separate query builders
164-
$nullParentQuery = $this->getQuery()->clone()->where($this->getParentAttribute(), $this->getParentNullValue());
165-
$nonNullParentQuery = $this->getQuery()->clone()->whereNot($this->getParentAttribute(), $this->getParentNullValue());
166-
167-
// If we're not at the root level and a modification callback is provided, apply it to null query
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
168165
if ($this->modifyQueryUsing) {
169-
$nullParentQuery = $this->evaluate($this->modifyQueryUsing, ['query' => $nullParentQuery]);
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);
170181
}
182+
183+
// Original logic for non-filtered queries
184+
$nullParentQuery = $this->getQuery()->clone()->where($this->getParentAttribute(), $this->getParentNullValue());
185+
$nonNullParentQuery = $this->getQuery()->clone()->whereNot($this->getParentAttribute(), $this->getParentNullValue());
171186

172187
// If we're at the child level and a modification callback is provided, apply it to non null query
173188
if ($this->modifyChildQueryUsing) {
@@ -223,10 +238,23 @@ private function buildTreeFromResults($results, $parent = null): Collection
223238

224239
// Recursively build the tree starting from the root (null parent)
225240
$rootResults = $resultMap[$parent] ?? [];
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);
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+
}
230258
}
231259

232260
return $tree;

0 commit comments

Comments
 (0)