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(),
]; }
]; } 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
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(),
];
}
MAke suire to wrap it in a closure so:
Ty. Appreciate it