Dependent select without parent being saved to the database

I have a simple form based on a WorkOrder. A WorkOrder belongs to an Equipment. An Equipment belongs to a Location. Therefore, a WorkOrder belongs to a location through an Equipment. I have a dependent select working for the most part:
Forms\Components\Select::make('location_id')
->label('Location')
->options(...)
->live()
->dehydrated(false)
->afterStateUpdated(fn (Forms\Set $set): null => $set('equipment_id', null))

Forms\Components\Select::make('equipment_id')
->label('Equipment')
->options(fn (Forms\Get $get): array => Equipment::query()->...),
Forms\Components\Select::make('location_id')
->label('Location')
->options(...)
->live()
->dehydrated(false)
->afterStateUpdated(fn (Forms\Set $set): null => $set('equipment_id', null))

Forms\Components\Select::make('equipment_id')
->label('Equipment')
->options(fn (Forms\Get $get): array => Equipment::query()->...),
My problem is on form hydration though when editing the WorkOrder. Since there is no location_id on the WorkOrder table, it obviously doesn't fill the location. I need to somehow fill the location based on the selected equipment instead and that's where I'm stuck. Any ideas?
No description
4 Replies
morty
mortyOP2w ago
To clarify, this is what it looks like when I edit the WorkOrder
morty
mortyOP2w ago
No description
toeknee
toeknee2w ago
Why about relationshiping it?
Group::make([
Forms\Components\Select::make('location_id')
->label('Location')
->options(...)
->live()
->dehydrated(false)
->afterStateUpdated(fn (Forms\Set $set): null => $set('equipment_id', null))

Forms\Components\Select::make('equipment_id')
->label('Equipment')
->options(fn (Forms\Get $get): array => Equipment::query()->...),
])
->relationship('equipment')
Group::make([
Forms\Components\Select::make('location_id')
->label('Location')
->options(...)
->live()
->dehydrated(false)
->afterStateUpdated(fn (Forms\Set $set): null => $set('equipment_id', null))

Forms\Components\Select::make('equipment_id')
->label('Equipment')
->options(fn (Forms\Get $get): array => Equipment::query()->...),
])
->relationship('equipment')
morty
mortyOP2w ago
This is for the location_id field though which doesn't technically have a belongsto relationship it's a belongsToThrough

Did you find this page helpful?