22 Replies
this is my code
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('car_brand_id')
->reactive()
->relationship('brand', 'name')
->searchable()
->options(Brand::limit(10)->pluck('name', 'id'))
->required(),
Forms\Components\Select::make('car_model_id')
->reactive()
->relationship('model', 'name')
->disabled(fn ($get) => $get('car_brand_id') == null ?? false)
->searchable()
->options(fn ($get) => CarModel::where('car_brand_id', $get('car_brand_id'))->limit(10)->pluck('name', 'id')),
Forms\Components\TextInput::make('name')
->required()
->unique(Type::class, 'name')
->columnSpan(2),
])->columns(2);
}
i want "Model" depend on "Brand", like:
1. if user select "brand" then "model" reset and must be select again
2. if user select "brand" (ex. TOYOTA) then "model" will load specific "model" data depend on selected "brand_id"
3. disabled/enabled "select model" if "select brand" is selected
your option from the second select is wrong, you need something like this:
->options(function (callable $get){
$customer = Customer::find($get('customers_id'));
if(!$customer){
return Buildings::all()->pluck('name', 'id');
}
return $customer->buildings->pluck('name', 'id'); }),
return $customer->buildings->pluck('name', 'id'); }),
okey, tkyou DonatasP and Xiquita it's work.
But i have another error, please help me again
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('car_brand_id')
->reactive()
->relationship('brand', 'name')
->searchable()
->options(Brand::limit(10)->pluck('name', 'id'))
->required()
->afterStateUpdated(fn ($set) => $set('car_model_id', null)),
Forms\Components\Select::make('car_model_id')
->searchable()
->reactive()
->disabled(function (callable $get) {
if (!$get('car_brand_id')) {
return true;
} else {
return false;
};
})
->required()
->options(function (callable $get) {
$brand = Brand::find($get('car_brand_id'));
if (!$brand) {
return CarModel::limit(10)->pluck('name', 'id');
}
return $brand->models->pluck('name', 'id');
}),
Forms\Components\TextInput::make('name')
->required()
->unique(Type::class, 'name')
->columnSpan(2),
])->columns(2);
}
disabled function is set to false but i can't select 'model' optionchange ->reactive() to ->live() since you are on V3
else, run some dd()'s on the options of the model_id select and ensure for example brand is being found
thankyou @toeknee for you answer, but i think that's is not my problem.. my problem is:
disabled function in my select "model" can't be selected when disabled set to false
Sorry it's far to early for me!
I can't remember if reactive is still supported in V3 so ensure you use live() as that replaces reactive.
Then disabled change to:
thankyou, but still not work
Forms\Components\Select::make('car_model_id')
->searchable()
->live()
// ->disabled(function (callable $get) {
// if (!$get('car_brand_id')) {
// return true;
// } else {
// return false;
// };
// })
->disabled(fn(callable $get) => empty($get('car_brand_id')))
->required()
->options(function (callable $get) {
$brand = Brand::find($get('car_brand_id'));
if (!$brand) {
return CarModel::limit(10)->pluck('name', 'id');
}
return $brand->models->pluck('name', 'id');
}),
To confirm you want car_model_id to be disabled until a brand is selected? And you understand that disabled means no select but is visible?
yes, i want car_model_id disabled until select brand_id
oh my God, disabled is mean no select but visible?
yes
so whats an optional? using hidden?
->options(function (callable $get) {
if (!$get('car_brand_id')) {
return;
} else {
$brand = Brand::find($get('car_brand_id'));
if (!$brand) {
return CarModel::limit(10)->pluck('name', 'id');
}
return $brand->models->pluck('name', 'id');
}
}),
or like this?
you want
sorry if me English bad. i'm from Indonesia
just updated it
thankyou @toeknee for you solution. i will use "visible"
probably also this on the car_brand_id select:
Okay, thankyou so much @toeknee
in V.3 using columnSpan
CMIIW
Yeah sorry, I'm still inbetween 2 and 3
okaay, tkyou all,, 🙏