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();
    }


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']);
            });

        }
    })


Also tried to create direct relation on the model like this but throws an error

  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
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();
    }
Was this page helpful?