Introduction
Filters allow you to define certain constraints on your data, and allow users to scope it to find the information they need. You put them in the$table->filters() method.
Filters may be created using the static make() method, passing its unique name. You should then pass a callback to query() which applies your filter’s scope:
Available filters
By default, using theFilter::make() method will render a checkbox form component. When the checkbox is on, the query() will be activated.
- You can also replace the checkbox with a toggle.
- You may use a select filter to allow users to select from a list of options, and filter using the selection.
- You can use a ternary filter to replace the checkbox with a select field to allow users to pick between 3 states - usually “true”, “false” and “blank”. This is useful for filtering boolean columns.
- The trashed filter is a pre-built ternary filter that allows you to filter soft-deletable records.
- Using a query builder, users can create complex sets of filters, with an advanced user interface for combining constraints.
- You may build custom filters with other form fields, to do whatever you want.
Setting a label
By default, the label of the filter is generated from the name of the filter. You may customize this using thelabel() method:
As well as allowing a static value, the label() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
label() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.Learn more about utility injection.
Filter
$filter
Filament\Tables\Filters\BaseFilter
The current filter instance.
Livewire
$livewire
Livewire\Component
The Livewire component instance.
Table
$table
Filament\Tables\Table
The current table instance.
Customizing the filter schema
By default, creating a filter with theFilter class will render a checkbox form component. When the checkbox is checked, the query() function will be applied to the table’s query, scoping the records in the table. When the checkbox is unchecked, the query() function will be removed from the table’s query.
Filters are built entirely on Filament’s form fields. They can render any combination of form fields, which users can then interact with to filter the table.
Using a toggle button instead of a checkbox
The simplest example of managing the form field that is used for a filter is to replace the checkbox with a toggle button, using thetoggle() method:
Customizing the built-in filter form field
Whether you are using a checkbox, a toggle or a select, you can customize the built-in form field used for the filter, using themodifyFormFieldUsing() method. The method accepts a function with a $field parameter that gives you access to the form field object to customize:
The function passed to modifyFormFieldUsing() can inject various utilities as parameters.
modifyFormFieldUsing() can inject various utilities as parameters.Learn more about utility injection.
Field
$field
Filament\Forms\Components\Field
The field object to modify.
Filter
$filter
Filament\Tables\Filters\BaseFilter
The current filter instance.
Livewire
$livewire
Livewire\Component
The Livewire component instance.
Table
$table
Filament\Tables\Table
The current table instance.
Applying the filter by default
You may set a filter to be enabled by default, using thedefault() method:
Persisting filters in the user’s session
To persist the table filters in the user’s session, use thepersistFiltersInSession() method:
Live filters
By default, filter changes are deferred and do not affect the table, until the user clicks an “Apply” button. To disable this and make the filters “live” instead, use thedeferFilters(false) method:
Customizing the apply filters action
When deferring filters, you can customize the “Apply” button, using thefiltersApplyAction() method, passing a closure that returns an action. All methods that are available to customize action trigger buttons can be used:
Deselecting records when filters change
By default, all records will be deselected when the filters change. Using thedeselectAllRecordsWhenFiltered(false) method, you can disable this behavior:
Modifying the base query
By default, modifications to the Eloquent query performed in thequery() method will be applied inside a scoped where() clause. This is to ensure that the query does not clash with any other filters that may be applied, especially those that use orWhere().
However, the downside of this is that the query() method cannot be used to modify the query in other ways, such as removing global scopes, since the base query needs to be modified directly, not the scoped query.
To modify the base query directly, you may use the baseQuery() method, passing a closure that receives the base query:
Excluding filters when resolving records
When a user interacts with a table record (e.g., clicking an action button), Filament resolves that record from the database. By default, all active filter conditions are applied, ensuring users cannot access records outside their filter scope. However, some filters likeTrashedFilter modify global scopes rather than restricting access. When a record’s state changes after the user saw it in the table, you may still want the user to interact with it.
You may mark a filter to be excluded when resolving records using the excludeWhenResolvingRecord() method:
excludeWhenResolvingRecord() is used:
- The filter’s
query()callback is not applied when resolving records - The filter’s
baseQuery()callback is still applied when resolving records
Do not use
excludeWhenResolvingRecord() on filters that enforce authorization rules. For example, if you have a filter that restricts records by tenant or user ownership, those filters should remain enforced to prevent unauthorized access.Customizing the filters trigger action
To customize the filters trigger buttons, you may use thefiltersTriggerAction() method, passing a closure that returns an action. All methods that are available to customize action trigger buttons can be used:
Filter utility injection
The vast majority of methods used to configure filters accept functions as parameters instead of hardcoded values:Injecting the current filter instance
If you wish to access the current filter instance, define a$filter parameter:
Injecting the current Livewire component instance
If you wish to access the current Livewire component instance that the table belongs to, define a$livewire parameter:
Injecting the current table instance
If you wish to access the current table configuration instance that the filter belongs to, define a$table parameter: