filters on same relationship

hi all, i've 2 Select filter on same relationship like this:
->filters([
SelectFilter::make('phisycalProfile')
->options(EyeColorEnum::keyValues())
->multiple()
->native(false)
->label('Eye color')
->query(fn (Builder $query, array $data): Builder => $query->when($data['values'],
fn (Builder $query) => $query->whereHas('phisycalProfile', function ($query) use ($data) {
$query->whereIn('eye_color', $data['values']);
}))
),
SelectFilter::make('phisycalProfile')
->options(HairColorEnum::keyValues())
->multiple()
->native(false)
->label('Hair color')
->query(fn (Builder $query, array $data): Builder => $query->when($data['values'],
fn (Builder $query) => $query->whereHas('phisycalProfile', function ($query) use ($data) {
$query->whereIn('hair_color', $data['values']);
}))
),
], layout: FiltersLayout::AboveContent)
->filters([
SelectFilter::make('phisycalProfile')
->options(EyeColorEnum::keyValues())
->multiple()
->native(false)
->label('Eye color')
->query(fn (Builder $query, array $data): Builder => $query->when($data['values'],
fn (Builder $query) => $query->whereHas('phisycalProfile', function ($query) use ($data) {
$query->whereIn('eye_color', $data['values']);
}))
),
SelectFilter::make('phisycalProfile')
->options(HairColorEnum::keyValues())
->multiple()
->native(false)
->label('Hair color')
->query(fn (Builder $query, array $data): Builder => $query->when($data['values'],
fn (Builder $query) => $query->whereHas('phisycalProfile', function ($query) use ($data) {
$query->whereIn('hair_color', $data['values']);
}))
),
], layout: FiltersLayout::AboveContent)
But the first SelectFilter "eye_color" isn't showed. how can i show the filter?
No description
2 Replies
Bdev
Bdev8mo ago
Hi ocram82, I believe select filters need to have distinct names provided to their make() method as internally data of the first Select Filter called phisycalProfile will be overwritten by the second one. best is to call them based on what data they should hold, then they will both display:
SelectFilter::make('eye_color')
->options( //rest of code here),
//and
SelectFilter::make('hair_color')
->options( //rest of code here)
SelectFilter::make('eye_color')
->options( //rest of code here),
//and
SelectFilter::make('hair_color')
->options( //rest of code here)
you can then specify in the select filters relationship they should apply to by using ->relationship('phisycalProfile') the relationship method will also require second parameter which is the property of relationship you want to filter by: ->relationship('phisycalProfile', 'eye_color') for example.
ocram82
ocram82OP8mo ago
yes it is, i solved creating 2 custom filters
Want results from more Discord servers?
Add your server