select depend by another select option

please help me!
No description
22 Replies
wandiaprianto
wandiapriantoβ€’9mo ago
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
Xiquita
Xiquitaβ€’9mo ago
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'); }),
wandiaprianto
wandiapriantoβ€’9mo ago
okey, tkyou DonatasP and Xiquita it's work. But i have another error, please help me again
No description
wandiaprianto
wandiapriantoβ€’9mo ago
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' option
toeknee
toekneeβ€’9mo ago
change ->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
wandiaprianto
wandiapriantoβ€’9mo ago
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
toeknee
toekneeβ€’9mo ago
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:
->disabled(fn(callable $get) => empty($get('car_brand_id')))
->disabled(fn(callable $get) => empty($get('car_brand_id')))
wandiaprianto
wandiapriantoβ€’9mo ago
thankyou, but still not work
No description
wandiaprianto
wandiapriantoβ€’9mo ago
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'); }),
toeknee
toekneeβ€’9mo ago
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?
wandiaprianto
wandiapriantoβ€’9mo ago
yes, i want car_model_id disabled until select brand_id oh my God, disabled is mean no select but visible?
toeknee
toekneeβ€’9mo ago
yes
wandiaprianto
wandiapriantoβ€’9mo ago
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?
toeknee
toekneeβ€’9mo ago
you want
->visible(fn(callable $get) => !empty($get('car_brand_id')))
->visible(fn(callable $get) => !empty($get('car_brand_id')))
wandiaprianto
wandiapriantoβ€’9mo ago
sorry if me English bad. i'm from Indonesia
toeknee
toekneeβ€’9mo ago
just updated it
wandiaprianto
wandiapriantoβ€’9mo ago
thankyou @toeknee for you solution. i will use "visible"
toeknee
toekneeβ€’9mo ago
probably also this on the car_brand_id select:
->columnSpan(fn(callable $get) => !empty($get('car_brand_id')) ? 1 : 2)
->columnSpan(fn(callable $get) => !empty($get('car_brand_id')) ? 1 : 2)
wandiaprianto
wandiapriantoβ€’9mo ago
Okay, thankyou so much @toeknee in V.3 using columnSpan CMIIW
toeknee
toekneeβ€’9mo ago
Yeah sorry, I'm still inbetween 2 and 3
wandiaprianto
wandiapriantoβ€’9mo ago
okaay, tkyou all,, πŸ™