SelectFilter relationship field

Hello all, I have a users table, with a relation (HasOne) user_meta table. So the relationship is: $user->meta->professional_category That returns the professional category of the user. So, why doesn't this filter work?
Tables\Filters\SelectFilter::make('professional_category')
->options(ProfessionalCategory::class) //Enum Class
->relationship('meta', 'professional_category')
->preload(),
Tables\Filters\SelectFilter::make('professional_category')
->options(ProfessionalCategory::class) //Enum Class
->relationship('meta', 'professional_category')
->preload(),
The output:
Filament\Forms\Components\Select::getRelationship(): Return value must be of type Illuminate\Database\Eloquent\Relations\BelongsTo|Illuminate\Database\Eloquent\Relations\BelongsToMany|Znck\Eloquent\Relations\BelongsToThrough|null, Illuminate\Database\Eloquent\Relations\HasOne returned
Filament\Forms\Components\Select::getRelationship(): Return value must be of type Illuminate\Database\Eloquent\Relations\BelongsTo|Illuminate\Database\Eloquent\Relations\BelongsToMany|Znck\Eloquent\Relations\BelongsToThrough|null, Illuminate\Database\Eloquent\Relations\HasOne returned
I don't know what I'm doing wrong. Must I run the query directly?
3 Replies
Diogo Pinto
Diogo PintoOP15mo ago
Also, to add that if I use:
Tables\Filters\SelectFilter::make('meta.professional_category'),
Tables\Filters\SelectFilter::make('meta.professional_category'),
The ouput is the same... I don't think my method worked. I can't combine multilple selects. Can anyone help?
awcodes
awcodes15mo ago
I’m guessing the issue is that the make on the SelectFilter is the name of your relationship and not the field name in the db. So you’re trying to relate a relationship to a relationship.
Diogo Pinto
Diogo PintoOP15mo ago
I got it to work, thanks @awcodes ! Here's how I got it:
->filters([
Tables\Filters\SelectFilter::make('professional_category')
->options(ProfessionalCategory::class)
->preload()
->native(false)
->query(
function (Builder $query, array $data): Builder {
return $query
->when(
$data['value'],
fn (Builder $query) => $query->whereRelation('meta', 'professional_category', $data['value']),
);
}
)
])
->filters([
Tables\Filters\SelectFilter::make('professional_category')
->options(ProfessionalCategory::class)
->preload()
->native(false)
->query(
function (Builder $query, array $data): Builder {
return $query
->when(
$data['value'],
fn (Builder $query) => $query->whereRelation('meta', 'professional_category', $data['value']),
);
}
)
])
Only when the data is selected is the filter triggered. Thanks 😁
Want results from more Discord servers?
Add your server