F
Filament15mo ago
Sugbo

How to dynamically populate select option label using the Form Builder

Hi, I am using the Select field of the form builder. I want to use the city names from my City model to populate the option label. If I have to do it manually it would look like this. protected function getFormSchema(): array { return [ Select::make('city') ->options([ 'miami' => 'Miami', 'boston' => 'Boston', 'denver' => 'Denver, ])->searchable(),
]; } What if I wanted to dynamically pull these city from the database. I assume it would look like this but obviously this does not work protected function getFormSchema(): array { return [ Select::make('city') ->options([ City::all()->pluck('city') ])->searchable(),
]; }
3 Replies
jlove1672
jlove167215mo ago
If you try City::query()->pluck('city', 'id')->toArray() that should work for you protected function getFormSchema(): array { return [ Select::make('city') ->options( City::query() ->pluck('city', 'id') ->toArray() ) ->searchable(), ]; }
toeknee
toeknee15mo ago
MAke suire to wrap it in a closure so:
protected function getFormSchema(): array
{
return [
Select::make('city')
->options(
fn() => City::query()
->pluck('city', 'id')
->toArray()
)
->searchable(),
];
}
protected function getFormSchema(): array
{
return [
Select::make('city')
->options(
fn() => City::query()
->pluck('city', 'id')
->toArray()
)
->searchable(),
];
}
Sugbo
Sugbo15mo ago
Ty. Appreciate it
Want results from more Discord servers?
Add your server
More Posts