Skip to content

Commit 7e57336

Browse files
committed
fix: use single query approach for modifyQueryUsing to handle filtered results properly
1 parent 04bb6a7 commit 7e57336

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/SelectTree.php

Lines changed: 21 additions & 6 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) {

0 commit comments

Comments
 (0)