filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.08k stars 2.83k forks source link

Filter for unique values is not working #8619

Closed alexmanase closed 12 months ago

alexmanase commented 1 year ago

Package

filament/filament

Package Version

v3.0.53

Laravel Version

v10.24.0

Livewire Version

v3.0.5

PHP Version

PHP 8.2.8

Problem description

I tried to create a filter that returns unique values for a query, like this:

Filter::make('is_unique')->query(function (Builder $query) {
    return $query->select()->groupBy('name');
})

Expected behavior

It has to correctly run the query from the Filter. When I run it in Tinker, it worked:

Product::select()->groupBy('name')->get()->count(); // 3

In the reproduction repository it should return 3 results, but instead it returns all of them.

Steps to reproduce

  1. Clone https://github.com/alexmanase/filament-filter-for-distinct-results-issue
  2. Follow the installation instructions from README.md
  3. Press the filter button and press "Is unique" filter.

Reproduction repository

https://github.com/alexmanase/filament-filter-for-distinct-results-issue

Relevant log output

No response

ilpav12 commented 12 months ago

@alexmanase if you change the method from query() to baseQuery() you will get the desired result as shown in the documentation https://filamentphp.com/docs/3.x/tables/filters#modifying-the-base-query. The final result will then be:

Filter::make('is_unique')
    ->baseQuery(fn (Builder $query): Builder => $query->groupBy('name')),
alexmanase commented 12 months ago

It works. Thank you!