F
Filament7mo ago
Abi

Modify query for Select Element on form with relationship based on another form element

I have 2 Select components on the form. Item Category and Vendor. both these fields have belongsToMany relationship with a pivot table category_vendor that stores vendors for each category or categories for a vendor. When a user selects a category, I want to display a list of vendors for that category and I am trying to use the modifyQueryUsing parameter for the relationship method on the vendor field. How do I pass the selected category to the vendor fields query parameter?
Select::make('item_category_id')
->relationship('itemCategory', 'name')
->createOptionAction(fn (Action $action) => $action->modalWidth('sm'))
->createOptionForm([
TextInput::make('name')
->label('Name')
->required(),
])
->preload()
->columnSpanFull()
->searchable()
->live()
->required(),

Select::make('vendor_id')
->relationship('vendor', 'name', fn ($query) => $query->whereHas('categories', '?')),
Select::make('item_category_id')
->relationship('itemCategory', 'name')
->createOptionAction(fn (Action $action) => $action->modalWidth('sm'))
->createOptionForm([
TextInput::make('name')
->label('Name')
->required(),
])
->preload()
->columnSpanFull()
->searchable()
->live()
->required(),

Select::make('vendor_id')
->relationship('vendor', 'name', fn ($query) => $query->whereHas('categories', '?')),
1 Reply
Povilas K
Povilas K7mo ago
Not entirely sure, but you should be able to pass (Get $get) in the fn and use $get('item_category_id')? Or maybe use a similar logic to this example from dependent dropdown we had in a demo project:
Forms\Components\Select::make('country_id')
->live()
->label('Country')
->dehydrated(false)
->options(Country::pluck('name', 'id'))
->afterStateUpdated(function (Livewire $livewire) {
$livewire->reset('data.city_id');
}),
Forms\Components\Select::make('city_id')
->required()
->label('City')
->placeholder(fn (Forms\Get $get): string => empty($get('country_id')) ? 'First select country' : 'Select an option')
->options(function (?Shop $record, Forms\Get $get, Forms\Set $set) {
if (! empty($record) && empty($get('country_id'))) {
$set('country_id', $record->city->country_id);
$set('city_id', $record->city_id);
}

return City::where('country_id', $get('country_id'))->pluck('name', 'id');
}),
Forms\Components\Select::make('country_id')
->live()
->label('Country')
->dehydrated(false)
->options(Country::pluck('name', 'id'))
->afterStateUpdated(function (Livewire $livewire) {
$livewire->reset('data.city_id');
}),
Forms\Components\Select::make('city_id')
->required()
->label('City')
->placeholder(fn (Forms\Get $get): string => empty($get('country_id')) ? 'First select country' : 'Select an option')
->options(function (?Shop $record, Forms\Get $get, Forms\Set $set) {
if (! empty($record) && empty($get('country_id'))) {
$set('country_id', $record->city->country_id);
$set('city_id', $record->city_id);
}

return City::where('country_id', $get('country_id'))->pluck('name', 'id');
}),
Want results from more Discord servers?
Add your server