F
Filament16mo ago
Numine

How do I use selectfilter to filter based on a column on another table.

I have two tables A and B which have an hasOne relationship. I want to use SelectFilter to filter table A based on a column in table B. I tried a CustomFilter by using a lookup table C to generate the content of a SelectForm and the writing a query to check for the column but I can't get relationships and joins to work. Please assist me. NB: I tested the query and join on artisan tinker and it works. And I am using v2.
Solution:
I have figured out the solution, needed a query to check the column based on the existing relationship: ``` // From the Resource for A SelectFilter::make('thename')...
Jump to solution
1 Reply
Solution
Numine
Numine16mo ago
I have figured out the solution, needed a query to check the column based on the existing relationship:

// From the Resource for A

SelectFilter::make('thename')
->multiple()
->options($theOptionsArray)
->query(
functions (Builder $query, array $data) {
if(count($data['values']) != 0)
$query->whereHas('relationBetweenA&B', fn (Builder $query) => $query->whereIn('columnOnB', $data['values']));
}
)

// From the Resource for A

SelectFilter::make('thename')
->multiple()
->options($theOptionsArray)
->query(
functions (Builder $query, array $data) {
if(count($data['values']) != 0)
$query->whereHas('relationBetweenA&B', fn (Builder $query) => $query->whereIn('columnOnB', $data['values']));
}
)
Want results from more Discord servers?
Add your server