Select Relationship Modify Query Using

Hi everyone, When I create an auction I select a car but the dropdown only shows the cars that don't belongs to another auction (I use modifyQueryUsing), so this works when I create. The issue is when I try to update an auction, I cannot see my car selected in the dropdown list because that car already belongs to the same auction that I'm updating so how can I fix that?
Select::make('car_id')
->label('Car')
->relationship(
name: 'car',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->whereDoesntHave('auction')
)
->required(),
Select::make('car_id')
->label('Car')
->relationship(
name: 'car',
titleAttribute: 'name',
modifyQueryUsing: fn (Builder $query) => $query->whereDoesntHave('auction')
)
->required(),
6 Replies
Garrita
Garrita2mo ago
Solved
Select::make('car_id')
->label('Car')
->relationship(
name: 'car',
titleAttribute: 'name',
)
->options(function (?Model $record): array {
return Car::query()
->doesntHave('auction')
->orWhere('id', $record?->car_id)
->get()
->pluck('name', 'id')
->toArray();
})
->required()
Select::make('car_id')
->label('Car')
->relationship(
name: 'car',
titleAttribute: 'name',
)
->options(function (?Model $record): array {
return Car::query()
->doesntHave('auction')
->orWhere('id', $record?->car_id)
->get()
->pluck('name', 'id')
->toArray();
})
->required()
krekas
krekas2mo ago
relationship() accepts more parameters one of which is modifyQueryUsing. Maybe using it it's possible to achieve the same result
krekas
krekas2mo ago
GitHub
filament/packages/forms/src/Components/Select.php at 3.x · filament...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
MohamedSabil83
MohamedSabil832mo ago
@Garrita use can check using $operation in ->modifyQueryUsing: fn (Builder $query, string $operation). It'll return the operation like create, edit, ... etc. You can modify according to that.
Garrita
Garrita2mo ago
I didn't know that we could inject those parameters in modifyQueryUsing omg, thanks!
MohamedSabil83
MohamedSabil832mo ago
you're welcome