@@ -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 $ record ->{ $ this -> getCustomKey ()} ;
115
117
});
116
118
117
119
$ this ->suffixActions ([
@@ -184,18 +186,18 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions
184
186
// Create a node with 'name' and 'value' attributes
185
187
$ node = [
186
188
'name ' => $ result ->{$ this ->getTitleAttribute ()},
187
- 'value ' => $ result ->getKey () ,
188
- 'disabled ' => in_array ($ result ->getKey () , $ disabledOptions ),
189
- 'hidden ' => in_array ($ result ->getKey () , $ hiddenOptions ),
189
+ 'value ' => $ result ->{ $ this -> getCustomKey ()} ,
190
+ 'disabled ' => in_array ($ result ->{ $ this -> getCustomKey ()} , $ disabledOptions ),
191
+ 'hidden ' => in_array ($ result ->{ $ this -> getCustomKey ()} , $ hiddenOptions ),
190
192
];
191
193
192
194
// Check if the result has children
193
- if (isset ($ resultMap [$ result ->getKey () ])) {
195
+ if (isset ($ resultMap [$ result ->{ $ this -> getCustomKey ()} ])) {
194
196
$ children = collect ();
195
197
// Recursively build child nodes
196
- foreach ($ resultMap [$ result ->getKey () ] as $ child ) {
198
+ foreach ($ resultMap [$ result ->{ $ this -> getCustomKey ()} ] as $ child ) {
197
199
// don't add the hidden ones
198
- if (in_array ($ child ->getKey () , $ hiddenOptions )) {
200
+ if (in_array ($ child ->{ $ this -> getCustomKey ()} , $ hiddenOptions )) {
199
201
continue ;
200
202
}
201
203
$ childNode = $ this ->buildNode ($ child , $ resultMap , $ disabledOptions , $ hiddenOptions );
@@ -301,6 +303,13 @@ public function independent(bool $independent = true): static
301
303
return $ this ;
302
304
}
303
305
306
+ public function withKey (string $ customKey = 'id ' ): static
307
+ {
308
+ $ this ->customKey = $ customKey ;
309
+
310
+ return $ this ;
311
+ }
312
+
304
313
public function disabledOptions (Closure |array $ disabledOptions ): static
305
314
{
306
315
$ this ->disabledOptions = $ disabledOptions ;
@@ -348,6 +357,11 @@ public function getIndependent(): bool
348
357
{
349
358
return $ this ->evaluate ($ this ->independent );
350
359
}
360
+
361
+ public function getCustomKey (): string
362
+ {
363
+ return $ this ->customKey ? $ this ->evaluate ($ this ->customKey ) : $ this ->getKey ();
364
+ }
351
365
352
366
public function getWithCount (): bool
353
367
{
0 commit comments