@@ -109,14 +109,46 @@ private function _shouldDisable(object $item, int|null $index = null): bool
109109 private function _getDataAttributes (mixed $ item , int |null $ index = null ): array
110110 {
111111 $ dataAttributes = [];
112- if (count ($ this ->_dataAttributes ) > 0 ){
113- foreach ($ this ->_dataAttributes as $ attribute => $ value ){
112+ if (count ($ this ->_dataAttributes ) > 0 ) {
113+ foreach ($ this ->_dataAttributes as $ attribute => $ value ) {
114114 $ dataAttributes [$ attribute ] = ($ value instanceof Closure) ? $ value ($ item ) : ($ item ->{$ value } ?? '' );
115115 }
116116 }
117117 return $ dataAttributes ;
118118 }
119119
120+ private function _generateOptions (Collection $ collection ): string
121+ {
122+ $ html = "" ;
123+ foreach ($ collection as $ index => $ item ) {
124+ if (($ item instanceof Collection)) { // Grouped options
125+ $ html .= "<optgroup label= \"{$ index }\"> " ;
126+ $ html .= $ this ->_generateOptions ($ item );
127+ $ html .= "</optgroup> " ;
128+ } else {
129+ $ optionLabel = ($ this ->_label instanceof Closure) ? call_user_func ($ this ->_label , $ item , $ index ) : $ item ->{$ this ->_label } ?? "N/A " ;
130+ $ optionValue = ($ this ->_value instanceof Closure) ? call_user_func ($ this ->_value , $ item , $ index ) : $ item ->{$ this ->_value } ?? "" ;
131+ $ html .= "<option value= \"{$ optionValue }\"" ;
132+ if ($ this ->_shouldSelect ($ item , $ index )) {
133+ $ html .= " selected " ;
134+ }
135+ if ($ this ->_shouldDisable ($ item , $ index )) {
136+ $ html .= " disabled " ;
137+ }
138+ if (count ($ this ->_dataAttributes ) > 0 ) {
139+ foreach ($ this ->_getDataAttributes ($ item , $ index ) as $ key => $ value ) {
140+ $ html .= " data- {$ key }= \"{$ value }\"" ;
141+ }
142+ }
143+ if (count ($ this ->_classes ) > 0 ) {
144+ $ html .= " class= \"" . (implode (' ' , $ this ->_classes )) . "\"" ;
145+ }
146+ $ html .= " > {$ optionLabel }</option> " ;
147+ }
148+ }
149+ return $ html ;
150+ }
151+
120152 /**
121153 * Generate select options from a Collection instance
122154 * @param Collection $collection the collection instance to be used
@@ -128,8 +160,8 @@ private function _getDataAttributes(mixed $item, int|null $index = null): array
128160 */
129161 public static function collectionToSelectOptions (
130162 Collection $ collection ,
131- string |null $ label = null ,
132- string |null $ value = null ,
163+ string |Closure | null $ label = null ,
164+ string |Closure | null $ value = null ,
133165 mixed $ selected = null ,
134166 mixed $ disabled = null ,
135167 ): string
@@ -153,28 +185,7 @@ public static function fromCollection(Collection $collection): self
153185 */
154186 public function toSelectOptions (): string
155187 {
156- $ html = "" ;
157- foreach ($ this ->_collection as $ index => $ item ) {
158- $ optionLabel = ($ this ->_label instanceof Closure) ? call_user_func ($ this ->_label , $ item , $ index ) : $ item ->{$ this ->_label } ?? "N/A " ;
159- $ optionValue = ($ this ->_value instanceof Closure) ? call_user_func ($ this ->_value , $ item , $ index ) : $ item ->{$ this ->_value } ?? "" ;
160- $ html .= "<option value= \"{$ optionValue }\"" ;
161- if ($ this ->_shouldSelect ($ item , $ index )) {
162- $ html .= " selected " ;
163- }
164- if ($ this ->_shouldDisable ($ item , $ index )) {
165- $ html .= " disabled " ;
166- }
167- if (count ($ this ->_dataAttributes ) > 0 ){
168- foreach ($ this ->_getDataAttributes ($ item , $ index ) as $ key => $ value ){
169- $ html .= " data- {$ key }= \"{$ value }\"" ;
170- }
171- }
172- if (count ($ this ->_classes ) > 0 ){
173- $ html .= " class= \"" . (implode (' ' , $ this ->_classes )) ."\"" ;
174- }
175- $ html .= " > {$ optionLabel }</option> " ;
176- }
177- return $ html ;
188+ return $ this ->_generateOptions ($ this ->_collection );
178189 }
179190
180191 /**
@@ -195,7 +206,6 @@ public function toSelectItems(): Collection
195206 });
196207 }
197208
198-
199209 /**
200210 * Specify the label for the selectable items
201211 * @param string|Closure $label name of the field to be used as label
@@ -254,7 +264,7 @@ public function withDataAttribute(string $attribute, string|Closure $value): sel
254264
255265 public function withClass (string $ class ): self
256266 {
257- $ this ->_classes = array_unique ([ ...$ this ->_classes , ...explode (' ' , $ class )]);
267+ $ this ->_classes = array_unique ([...$ this ->_classes , ...explode (' ' , $ class )]);
258268 return $ this ;
259269 }
260270
@@ -267,4 +277,21 @@ public function toCollection(): Collection
267277 return $ this ->_collection ;
268278 }
269279
280+ public function __call (string $ name , array $ arguments )
281+ {
282+ $ allowedMethods = [
283+ 'groupBy ' , 'add ' , 'zip ' , 'unique ' , 'range ' ,
284+ 'diff ' , 'diffUsing ' , 'diffAssoc ' , 'diffAssocUsing ' ,
285+ 'diffKeys ' , 'diffKeysUsing ' , 'forget '
286+ ];
287+
288+ if (in_array ($ name , $ allowedMethods ) && method_exists ($ this ->_collection , $ name )) {
289+ $ res = $ this ->_collection ->{$ name }(...$ arguments );
290+ if ($ res instanceof Collection) {
291+ $ this ->_collection = $ res ;
292+ }
293+ }
294+ return $ this ;
295+ }
296+
270297}
0 commit comments