James Kenworthy
James Kenworthy
FFilament
Created by James Kenworthy on 11/17/2024 in #❓┊help
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!
4 replies