@@ -29,7 +29,9 @@ class SelectTree extends Field
2929
3030 protected bool $ disabledBranchNode = false ;
3131
32- protected string $ treeModel ;
32+ protected ?string $ treeModel = null ;
33+
34+ protected array $ options = [];
3335
3436 protected string $ treeParentKey ;
3537
@@ -72,14 +74,21 @@ public function multiple(bool $multiple = true): static
7274 return $ this ;
7375 }
7476
77+ public function options (array $ options ): static
78+ {
79+ $ this ->options = $ options ;
80+
81+ return $ this ;
82+ }
83+
7584 public function disabledBranchNode (bool $ disabledBranchNode = true ): static
7685 {
7786 $ this ->disabledBranchNode = $ disabledBranchNode ;
7887
7988 return $ this ;
8089 }
8190
82- public function getTree (): Collection
91+ public function getTree (): Collection | array
8392 {
8493 return $ this ->evaluate ($ this ->buildTree ());
8594 }
@@ -109,6 +118,11 @@ public function getAlwaysOpen(): bool
109118 return $ this ->evaluate ($ this ->alwaysOpen );
110119 }
111120
121+ public function getOptions (): array
122+ {
123+ return $ this ->evaluate ($ this ->options );
124+ }
125+
112126 public function getDisabledBranchNode (): bool
113127 {
114128 return $ this ->evaluate ($ this ->disabledBranchNode );
@@ -124,13 +138,23 @@ public function tree(string $treeModel, string $treeParentKey, string $titleAttr
124138 return $ this ;
125139 }
126140
127- private function buildTree (int $ parent = null ): Collection
141+ private function buildTree (int $ parent = null ): array | Collection
128142 {
143+ // Check if custom options are set; if yes, return them.
144+ if ($ this ->getOptions ()) {
145+ return $ this ->getOptions ();
146+ }
147+
148+ // Check if the treeModel is not set; if yes, return an empty collection.
149+ if (! $ this ->treeModel ) {
150+ return collect ();
151+ }
152+
129153 // Create a default query to fetch items with the specified parent ID.
130154 $ defaultQuery = $ this ->treeModel ::query ()
131155 ->where ($ this ->treeParentKey , $ parent );
132156
133- // If not a root level query and a modification callback is provided, apply it.
157+ // If we're not at the root level and a modification callback is provided, apply it.
134158 if (! $ parent && $ this ->modifyQueryUsing ) {
135159 $ defaultQuery = $ this ->evaluate ($ this ->modifyQueryUsing , ['query ' => $ defaultQuery ]);
136160 }
0 commit comments