Select
You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.
-
Sections
The text field includes all the basic methods and additional methods for select fields
use MoonShine\Fields\Select; //... public function fields(): array{ return [ Select::make('Country', 'country_id') ->options([ 'value 1' => 'Option Label 2', 'value 2' => 'Option Label 2' ]) ];} //...
# Nullable
If you need to save NULL, then you need to add a nullable
method
Select::make('Country', 'country_id') ->nullable()
# Groups
You can combine values into groups
Select::make('City')->options([ 'Italy' => [ 1 => 'Rome', 2 => 'Milan' ], 'France' => [ 3 => 'Paris', 4 => 'Marseille' ],]),
# Multiple select
To select multiple values, you need the multiple
method
Select::make('Country', 'country_id') ->multiple()
The field in the database must be of the text or json type.
You also need to add a cast for the eloquent model - json or array or collection.
# Search
If you want to add a search among values, you need to add the searchable
method
Select::make('Country', 'country_id') ->searchable()