@@ -37,6 +37,8 @@ class SelectTree extends Field implements HasAffixActions
37
37
38
38
protected bool $ independent = true ;
39
39
40
+ protected ?string $ customKey = null ;
41
+
40
42
protected string $ titleAttribute ;
41
43
42
44
protected string $ parentAttribute ;
@@ -111,7 +113,7 @@ protected function setUp(): void
111
113
112
114
$ form ->model ($ record )->saveRelationships ();
113
115
114
- return $ record -> getKey ( );
116
+ return $ this -> getCustomKey ( $ record );
115
117
});
116
118
117
119
$ this ->suffixActions ([
@@ -181,22 +183,24 @@ private function buildTreeFromResults($results, $parent = null): Collection
181
183
182
184
private function buildNode ($ result , $ resultMap , $ disabledOptions , $ hiddenOptions ): array
183
185
{
186
+ $ key = $ this ->getCustomKey ($ result );
187
+
184
188
// Create a node with 'name' and 'value' attributes
185
189
$ node = [
186
190
'name ' => $ result ->{$ this ->getTitleAttribute ()},
187
- 'value ' => $ result -> getKey () ,
191
+ 'value ' => $ key ,
188
192
'parent ' => $ result ->{$ this ->getParentAttribute ()},
189
- 'disabled ' => in_array ($ result -> getKey () , $ disabledOptions ),
190
- 'hidden ' => in_array ($ result -> getKey () , $ hiddenOptions ),
193
+ 'disabled ' => in_array ($ key , $ disabledOptions ),
194
+ 'hidden ' => in_array ($ key , $ hiddenOptions ),
191
195
];
192
196
193
197
// Check if the result has children
194
- if (isset ($ resultMap [$ result -> getKey () ])) {
198
+ if (isset ($ resultMap [$ key ])) {
195
199
$ children = collect ();
196
200
// Recursively build child nodes
197
- foreach ($ resultMap [$ result -> getKey () ] as $ child ) {
201
+ foreach ($ resultMap [$ key ] as $ child ) {
198
202
// don't add the hidden ones
199
- if (in_array ($ child -> getKey ( ), $ hiddenOptions )) {
203
+ if (in_array ($ this -> getCustomKey ( $ child ), $ hiddenOptions )) {
200
204
continue ;
201
205
}
202
206
$ childNode = $ this ->buildNode ($ child , $ resultMap , $ disabledOptions , $ hiddenOptions );
@@ -302,6 +306,13 @@ public function independent(bool $independent = true): static
302
306
return $ this ;
303
307
}
304
308
309
+ public function withKey (string $ customKey ): static
310
+ {
311
+ $ this ->customKey = $ customKey ;
312
+
313
+ return $ this ;
314
+ }
315
+
305
316
public function disabledOptions (Closure |array $ disabledOptions ): static
306
317
{
307
318
$ this ->disabledOptions = $ disabledOptions ;
@@ -350,6 +361,11 @@ public function getIndependent(): bool
350
361
return $ this ->evaluate ($ this ->independent );
351
362
}
352
363
364
+ public function getCustomKey ($ record )
365
+ {
366
+ return is_null ($ this ->customKey ) ? $ record ->getKey () : $ record ->{$ this ->customKey };
367
+ }
368
+
353
369
public function getWithCount (): bool
354
370
{
355
371
return $ this ->evaluate ($ this ->withCount );
0 commit comments