1 Select input depends on another Select input's value. Select's options populated by DB table's col

Currently I got this
return $form
->schema([

Forms\Components\Select::make('model')
Forms\Components\Select::make('model_id')
return $form
->schema([

Forms\Components\Select::make('model')
Forms\Components\Select::make('model_id')
Model is a select menu, it can be Aaa or Bbb. Model_id is also a select menu, How can you achieve the below: If Model is selected to have a value of Aaa, then Model_id's options will be:
1: aaa1
2: aaa2
1: aaa1
2: aaa2
1:aaa1 and 2:aaa2 are read from DB table aaas's id and name columns. If Model is selected to have a value of Bbb, then Model_id's options will be:
1: bbb1
2: bbb2
1: bbb1
2: bbb2
1:bbb1 and 2:bbb2 are read from DB table bbbs's id and name columns.
3 Replies
Stfn
Stfn4mo ago
Hmm im not 100% sure, but i think this might be helpful for you.. i usually do something like this:
Forms\Components\Select::make('merchant_id')
->label('Merchant')
->searchable()
->options(
Merchant::all()->pluck('name', 'id')->toArray()
)
Forms\Components\Select::make('merchant_id')
->label('Merchant')
->searchable()
->options(
Merchant::all()->pluck('name', 'id')->toArray()
)
Where it plucks the data from the DB, but i'm not 100% on how you set it to the value
JustNoOne
JustNoOne4mo ago
should be something like this use Filament\Forms\Get; use App\Models\aaas; use App\Models\bbbs;
Forms\Components\Select::make('model') ->options([ 'Aaa' => 'Aaa', 'Bbb' => 'Bbb' ]), Forms\Components\Select::make('model_id') ->options([ function (Get $get){ switch ($get('model')){ case 'Aaa': return aaas::all()->pluck('name', 'id'); case 'Bbb': return bbbs::all()->pluck('name', 'id'); default: } } ]),
atabegruslan
atabegruslanOP4mo ago
Yes, the solution is good, except it does not update "on the fly" I have to save it first, then come back to edit it Is there a better way?
Want results from more Discord servers?
Add your server