@@ -47,62 +47,71 @@ class SelectTree extends Field
4747
4848 protected ?Closure $ modifyQueryUsing ;
4949
50- public function withCount (bool $ withCount = true ): static
51- {
52- $ this ->withCount = $ withCount ;
53-
54- return $ this ;
55- }
56-
5750 protected function setUp (): void
5851 {
59- $ this ->relationship = $ this ->getName ();
60-
52+ // Load the state from relationships using a callback function.
6153 $ this ->loadStateFromRelationshipsUsing (static function (self $ component ): void {
54+ // Get the current relationship associated with the component.
6255 $ relationship = $ component ->getRelationship ();
63- if ( $ relationship instanceof BelongsTo) {
64- $ component -> state ( $ relationship-> getResults ()?->getKey());
65- } else {
66- $ results = $ relationship -> getResults ();
67- $ state = $ results
56+
57+ // Check if the relationship is a BelongsToMany relationship.
58+ if ( $ relationship instanceof BelongsToMany) {
59+ // Retrieve related model instances and extract their IDs into an array.
60+ $ state = $ relationship -> getResults ()
6861 ->pluck ($ relationship ->getRelatedKeyName ())
6962 ->toArray ();
63+
64+ // Set the component's state with the extracted IDs.
7065 $ component ->state ($ state );
7166 }
7267 });
7368
69+ // Save relationships using a callback function.
7470 $ this ->saveRelationshipsUsing (static function (self $ component , $ state ) {
71+ // Check if the component's relationship is a BelongsToMany relationship.
7572 if ($ component ->getRelationship () instanceof BelongsToMany) {
73+ // Wrap the state in a collection and convert it to an array if it's not set.
7674 $ state = Collection::wrap ($ state ?? []);
75+
76+ // Sync the relationship with the provided state (IDs).
7777 $ component ->getRelationship ()->sync ($ state ->toArray ());
78+
79+ // Set the component as not dehydrated (assuming this is related to the framework's functionality).
7880 $ component ->dehydrated (false );
7981 }
8082 });
81-
8283 }
8384
8485 private function buildTree (int $ parent = null ): array |Collection
8586 {
86-
87+ // Check if options have already been set, if so, return them.
8788 if ($ this ->getOptions ()) {
8889 return $ this ->getOptions ();
8990 }
9091
92+ // Determine the foreign key based on the type of relationship.
9193 if ($ this ->getRelationship () instanceof BelongsTo) {
9294 $ key = $ this ->getRelationship ()->getForeignKeyName ();
9395 }
9496
97+ // Determine the related pivot key for BelongsToMany relationships.
9598 if ($ this ->getRelationship () instanceof BelongsToMany) {
9699 $ key = $ this ->getRelationship ()->getRelatedPivotKeyName ();
97100 }
98101
102+ // If the key is not set, return an empty array.
103+ if (!isset ($ key )) {
104+ return [];
105+ }
106+
107+ // Create a default query to retrieve related items.
99108 $ defaultQuery = $ this ->getRelationship ()->getRelated ()->query ()
100109 ->where ($ key , $ parent );
101110
102- // // If we're not at the root level and a modification callback is provided, apply it.
103- // if (!$parent && $this->modifyQueryUsing) {
104- // $defaultQuery = $this->evaluate($this->modifyQueryUsing, ['query' => $defaultQuery]);
105- // }
111+ // If we're not at the root level and a modification callback is provided, apply it to the query .
112+ if (!$ parent && $ this ->modifyQueryUsing ) {
113+ $ defaultQuery = $ this ->evaluate ($ this ->modifyQueryUsing , ['query ' => $ defaultQuery ]);
114+ }
106115
107116 // Fetch the results from the default query.
108117 $ results = $ defaultQuery ->get ();
@@ -115,17 +124,25 @@ private function buildTree(int $parent = null): array|Collection
115124
116125 // Create an array representation of the current result with children.
117126 return [
118- 'name ' => $ result ->name ,
127+ 'name ' => $ result ->{ $ this -> getTitleAttribute ()} ,
119128 'value ' => $ result ->id ,
120129 'children ' => $ children ->isEmpty () ? null : $ children ->toArray (),
121130 ];
122131 });
123132 }
124133
125- public function relationship (string $ relationship , string $ titleAttribute ): self
134+ public function relationship (string $ relationship , string $ titleAttribute, ? Closure $ modifyQueryUsing = null ): self
126135 {
127136 $ this ->relationship = $ relationship ;
128137 $ this ->titleAttribute = $ titleAttribute ;
138+ $ this ->modifyQueryUsing = $ modifyQueryUsing ;
139+
140+ return $ this ;
141+ }
142+
143+ public function withCount (bool $ withCount = true ): static
144+ {
145+ $ this ->withCount = $ withCount ;
129146
130147 return $ this ;
131148 }
0 commit comments