@@ -60,6 +60,8 @@ class SelectTree extends Field implements HasAffixActions
60
60
61
61
protected ?Closure $ modifyQueryUsing ;
62
62
63
+ protected ?Closure $ modifyChildQueryUsing ;
64
+
63
65
protected Closure |int $ defaultOpenLevel = 0 ;
64
66
65
67
protected string $ direction = 'auto ' ;
@@ -146,6 +148,11 @@ private function buildTree(): Collection
146
148
$ nullParentQuery = $ this ->evaluate ($ this ->modifyQueryUsing , ['query ' => $ nullParentQuery ]);
147
149
}
148
150
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
+
149
156
if ($ this ->withTrashed ) {
150
157
$ nullParentQuery ->withTrashed ($ this ->withTrashed );
151
158
$ nonNullParentQuery ->withTrashed ($ this ->withTrashed );
@@ -156,7 +163,7 @@ private function buildTree(): Collection
156
163
157
164
// Combine the results from both queries
158
165
$ combinedResults = $ nullParentResults ->concat ($ nonNullParentResults );
159
-
166
+
160
167
return $ this ->buildTreeFromResults ($ combinedResults );
161
168
}
162
169
@@ -231,13 +238,14 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions
231
238
return $ node ;
232
239
}
233
240
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
235
242
{
236
243
$ this ->relationship = $ relationship ;
237
244
$ this ->titleAttribute = $ titleAttribute ;
238
245
$ this ->parentAttribute = $ parentAttribute ;
239
246
$ this ->modifyQueryUsing = $ modifyQueryUsing ;
240
-
247
+ $ this ->modifyChildQueryUsing = $ modifyChildQueryUsing ;
248
+
241
249
return $ this ;
242
250
}
243
251
@@ -370,7 +378,7 @@ public function getTree(): Collection|array
370
378
{
371
379
return $ this ->evaluate ($ this ->buildTree ());
372
380
}
373
-
381
+
374
382
public function getExpandSelected (): bool
375
383
{
376
384
return $ this ->evaluate ($ this ->expandSelected );
0 commit comments