On a create form, I have a 3-tiered dependent dropdown.
For example:
On a Person model I might have the following Country -> State/Province -> City
In the database on the persons table I'm only saving the city_id since I can always look up the state and country via the city.
I have this working on the create form but when someone clicks to edit the form, how can I fill in the country_id and state_province_id since these don't exist from the data?
My code:
Forms\Components\Select::make('country_id')
->label('Country')
->live()
->relationship('country', 'name')
->searchable()
->preload()
->afterStateUpdated(function (Forms\Set $set): void {
$set('state_province_id', null);
$set('city_id', null);
})
->dehydrated(false),
Forms\Components\Select::make('state_province_id')
->label('State/Province')
->live()
->disabled(fn (Forms\Get $get): bool => ! filled($get('country_id')))
->relationship('stateProvince', 'name')
->searchable()
->preload()
->afterStateUpdated(function (Forms\Set $set): void {
$set('city_id', null);
})
->dehydrated(false),
Forms\Components\Select::make('city_id')
->label('City')
->relationship('city', 'name')
->searchable()
->preload()
->required()
->exists(City::class, 'id'),