Skip to content

Commit afde1cf

Browse files
Merge pull request #97 from iotronlab/patch-2
2 parents b878bb7 + fce5d2b commit afde1cf

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/SelectTree.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class SelectTree extends Field implements HasAffixActions
6060

6161
protected ?Closure $modifyQueryUsing;
6262

63+
protected ?Closure $modifyChildQueryUsing;
64+
6365
protected Closure|int $defaultOpenLevel = 0;
6466

6567
protected string $direction = 'auto';
@@ -146,6 +148,11 @@ private function buildTree(): Collection
146148
$nullParentQuery = $this->evaluate($this->modifyQueryUsing, ['query' => $nullParentQuery]);
147149
}
148150

151+
// If we're at the child level and a modification callback is provided, apply it to non null query
152+
if ($this->modifyChildQueryUsing) {
153+
$nonNullParentQuery = $this->evaluate($this->modifyChildQueryUsing, ['query' => $nonNullParentQuery]);
154+
}
155+
149156
if ($this->withTrashed) {
150157
$nullParentQuery->withTrashed($this->withTrashed);
151158
$nonNullParentQuery->withTrashed($this->withTrashed);
@@ -156,7 +163,7 @@ private function buildTree(): Collection
156163

157164
// Combine the results from both queries
158165
$combinedResults = $nullParentResults->concat($nonNullParentResults);
159-
166+
160167
return $this->buildTreeFromResults($combinedResults);
161168
}
162169

@@ -231,13 +238,14 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions
231238
return $node;
232239
}
233240

234-
public function relationship(string $relationship, string $titleAttribute, string $parentAttribute, ?Closure $modifyQueryUsing = null): self
241+
public function relationship(string $relationship, string $titleAttribute, string $parentAttribute, ?Closure $modifyQueryUsing = null, ?Closure $modifyChildQueryUsing = null): self
235242
{
236243
$this->relationship = $relationship;
237244
$this->titleAttribute = $titleAttribute;
238245
$this->parentAttribute = $parentAttribute;
239246
$this->modifyQueryUsing = $modifyQueryUsing;
240-
247+
$this->modifyChildQueryUsing = $modifyChildQueryUsing;
248+
241249
return $this;
242250
}
243251

@@ -370,7 +378,7 @@ public function getTree(): Collection|array
370378
{
371379
return $this->evaluate($this->buildTree());
372380
}
373-
381+
374382
public function getExpandSelected(): bool
375383
{
376384
return $this->evaluate($this->expandSelected);

0 commit comments

Comments
 (0)