The Filter class provides simple filtering functionality for Filament tables. It creates a checkbox or toggle-based filter that can be used to filter table records.
Namespace
Filament\Tables\Filters\Filter
Inheritance
Extends: Filament\Tables\Filters\BaseFilter
Usage
The Filter class creates a simple boolean filter with a checkbox or toggle interface.
Example:
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
Filter::make('is_featured')
->label('Featured')
->query(fn (Builder $query): Builder => $query->where('is_featured', true));
Methods
toggle()
Use a toggle switch instead of a checkbox.
public function toggle(): static
Returns the filter instance for method chaining.
Example:
Filter::make('is_active')
->toggle()
->query(fn (Builder $query): Builder => $query->where('is_active', true));
checkbox()
Use a checkbox (default behavior).
public function checkbox(): static
Returns the filter instance for method chaining.
Set a custom form component for the filter.
public function formComponent(string $component): static
The fully qualified class name of a field component (e.g., Checkbox::class or Toggle::class).
Returns the filter instance for method chaining.
Example:
use Filament\Forms\Components\Checkbox;
Filter::make('verified')
->formComponent(Checkbox::class)
->query(fn (Builder $query): Builder => $query->whereNotNull('email_verified_at'));
Get the form field component for this filter.
public function getFormField(): Field
Returns the configured form field component.
getResetState()
Get the state to use when resetting the filter.
public function getResetState(): array
Returns an array with the reset state (typically ['isActive' => false]).
Inherited Methods
The Filter class inherits methods from BaseFilter:
make()
Create a new filter instance.
public static function make(string $name): static
The unique identifier for this filter.
Returns the filter instance.
label()
Set the filter label.
public function label(string | Htmlable | Closure | null $label): static
label
string | Htmlable | Closure | null
The label to display for this filter.
Returns the filter instance for method chaining.
query()
Define the query modification when the filter is active.
public function query(Closure | null $callback): static
A callback that receives the query builder and modifies it. The callback should accept Builder $query and optionally array $state.
Returns the filter instance for method chaining.
Example:
Filter::make('verified')
->query(fn (Builder $query): Builder => $query->whereNotNull('email_verified_at'));
default()
Set the filter as active by default.
public function default(bool | Closure $condition = true): static
condition
bool | Closure
default:"true"
Whether the filter should be active by default.
Returns the filter instance for method chaining.
Example:
Filter::make('active')
->default()
->query(fn (Builder $query): Builder => $query->where('status', 'active'));
indicator()
Set a custom indicator for when the filter is active.
public function indicator(string | Indicator | Closure $indicator): static
indicator
string | Indicator | Closure
The indicator text or an Indicator instance.
Returns the filter instance for method chaining.
Example:
Filter::make('is_featured')
->indicator('Featured items')
->query(fn (Builder $query): Builder => $query->where('is_featured', true));
Filter State
The Filter class manages a simple state structure:
[
'isActive' => true, // or false
]
You can access this state in the query callback:
Filter::make('verified')
->query(function (Builder $query, array $state): Builder {
if ($state['isActive'] ?? false) {
return $query->whereNotNull('email_verified_at');
}
return $query;
});
Complete Example
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
Filter::make('is_featured')
->label('Featured Posts')
->toggle()
->default()
->query(fn (Builder $query): Builder => $query->where('is_featured', true))
->indicator('Showing featured posts');
Properties
protected string $formComponent = Checkbox::class;
The form component class to use for the filter input.
- Table - Table builder
- Column - Table columns
BaseFilter - Base filter class with additional functionality
SelectFilter - Filter with select dropdown
TernaryFilter - Three-state filter (yes/no/all)