1 Select input depends on another Select input's value. Select's options populated by DB table's col
Currently I got this
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
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
and 2:bbb2
are read from DB table bbbs
's id
and name
columns.3 Replies
Hmm im not 100% sure, but i think this might be helpful for you..
i usually do something like this:
Where it plucks the data from the DB, but i'm not 100% on how you set it to the value
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: } } ]),
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: } } ]),
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?