Dependent Select -> Form Fill Issue

I have a form which contains 2 dependent select fields. On create the fields work perfectly, however on edit I can not for the life of me get these fields to populate with the saved data. In my mount method I have added logging and in the ->afterStateHyrated I have added logging and everything shows that the fields are being populated however when the modal opens the fields are not selected... here is my code: my form fields in question: Division>Location>Resource Select::make('division_id') ->label('Division') ->options(Division::where('company_id', session('selected_company_id'))->pluck('name', 'id')) ->required() ->live(), Select::make('location_id') ->label('Location') ->placeholder(fn (Get $get): string => empty($get('division_id')) ? 'First select division' : 'Select an option') ->required() ->live() ->options(fn (Get $get): \Illuminate\Support\Collection => $get('division_id') ? Location::where('division_id', $get('division_id')) ->where('company_id', session('selected_company_id')) ->pluck('name', 'id') : collect([]) ) ->afterStateHydrated(function (Set $set, Get $get, ?Model $record) { if ($record) { $locationOptions = Location::where('division_id', $get('division_id')) ->where('company_id', session('selected_company_id')) ->pluck('name', 'id'); $set('location_id', $record->location_id); $set('options', $locationOptions); Log::info("Location field hydrated with options", [ 'options' => $locationOptions, 'selected' => $record->location_id, ]); } else { Log::info("Location field NOT hydrated - No record found."); } }), Select::make('resource_id') ->label('Resource') ->nullable() ->live() ->options(fn (Get $get): \Illuminate\Support\Collection => $get('location_id') ? Resource::where('location_id', $get('location_id'))->pluck('name', 'id') : collect([]) ) ->afterStateHydrated(function (Set $set, Get $get, ?Model $record) { if ($record) { $resourceOptions = Resource::where('location_id', $get('location_id'))->pluck('name', 'id'); $set('resource_id', $record->resource_id); $set('options', $resourceOptions); Log::info("Resource field hydrated with options", [ 'options' => $resourceOptions, 'selected' => $record->resource_id, ]); } else { Log::info("Resource field NOT hydrated - No record found."); } }), any adivce? I'm pulling out what little hair I have left!
2 Replies
bardolf_6969
bardolf_69693w ago
Either add your relationship
Select::make('resource_id')->relationship('resource', 'name')
Select::make('resource_id')->relationship('resource', 'name')
just guessing on the title field. Or you will need to a
->beforeStateHydrate
->beforeStateHydrate
function to look up the values
James Kenworthy
James KenworthyOP3w ago
the solutions was a teak to the ->afterStateHyrdated and adding the relationship.. Select::make('location_id') ->label('Location') ->preload() ->relationship(name: 'location', titleAttribute: 'name') ->placeholder(fn (Get $get): string => empty($get('division_id')) ? 'First select division' : 'Select an option') ->required() ->live() ->afterStateHydrated(function (Set $set, Get $get, ?Model $record) { if ($record) { $set('location_id', $record->location_id); Log::info("Location field hydrated with selected value", [ 'selected' => $record->location_id, ]); } }), Select::make('resource_id') ->preload() ->relationship(name: 'resource', titleAttribute: 'name') ->label('Resource') ->nullable() ->live() ->afterStateHydrated(function (Set $set, Get $get, ?Model $record) { if ($record) { $set('resource_id', $record->resource_id); Log::info("Resource field hydrated with selected value", [ 'selected' => $record->resource_id, ]); } }),
Want results from more Discord servers?
Add your server