Select Filter with nested relation won't filter

public static function productFilter($relationship = 'lineItem.product'){
return SelectFilter::make($relationship)
->label('Product')
->relationship($relationship, 'name')
->native(false)
->preload()
->searchable();
}
public static function productFilter($relationship = 'lineItem.product'){
return SelectFilter::make($relationship)
->label('Product')
->relationship($relationship, 'name')
->native(false)
->preload()
->searchable();
}
If we use a direct relationship the select filter works Nested relations provide the correct dropdown options but does not filter the results If try to add a query or ->modifyQueryUsing $data array is never populated.
->query(function (Builder $query, array $data) use ($relationship) {
if (isset($data['value'])) {
$query->whereHas($relationship, function ($query) use ($data) {
$query->where('id', $data['value']);
});

}
})
->query(function (Builder $query, array $data) use ($relationship) {
if (isset($data['value'])) {
$query->whereHas($relationship, function ($query) use ($data) {
$query->where('id', $data['value']);
});

}
})
Also tried to create direct relation on the model like this but throws an error
public function productCategory(): BelongsTo
{
return $this->product()->productCategory();
}
public function productCategory(): BelongsTo
{
return $this->product()->productCategory();
}
Solution:
Found the problem. the Select filter name needs to be the attribute name
product_category_id
product_category_id
like this:- ```php public static function productCategoryFilter($relationship = 'product.productCategory'){ return SelectFilter::make('product_category_id')...
Jump to solution
2 Replies
acroninja
acroninjaOP6mo ago
Annoyingly I can see the data in the Get request but this is not populated when the select is first changed so can't do it via these
No description
Solution
acroninja
acroninja6mo ago
Found the problem. the Select filter name needs to be the attribute name
product_category_id
product_category_id
like this:-
public static function productCategoryFilter($relationship = 'product.productCategory'){
return SelectFilter::make('product_category_id')
->label('Product Category')
->relationship($relationship, 'title')
->native(false)
->searchable()
->preload();
}
public static function productCategoryFilter($relationship = 'product.productCategory'){
return SelectFilter::make('product_category_id')
->label('Product Category')
->relationship($relationship, 'title')
->native(false)
->searchable()
->preload();
}
Want results from more Discord servers?
Add your server