Everything you need to know about Laravel Filament Filters

Filament is a powerful admin panel package for laravel that provides a wide range of filters to apply filtering on the tables. In this article, we are exploring the different types of laravel filament filters in details.

Types of Laravel Filament Filters

There are mainly 3 types of laravel filament filters. They are as:

  1. Select Filters
  2. Ternary Filters
  3. Query Builder

If you need a different type, don’t worry! Filament’s built-in methods make it easy to create custom Laravel Filament filters. Let’s dive them in detail.

Check more more filament tips and tricks.

Ternary Filters

The name itself gives it away—ternary filters are used to filter records based on whether an attribute is true or false. This type of filter are very easy to implement. You can directly import TernaryFilter class and pass the attribute name inside that and see the output.

........
->filters([
    TernaryFilter::make('is_admin')
])
......

Not only that you can customize it more and more. If you want to add the label for specific value or placeholder you can also do that easily. Just check the code below:

......
->filters([
TernaryFilter::make('email_verified_at')
    ->label('Email verification')
    ->nullable()
    ->placeholder('All users')
    ->trueLabel('Verified users')
    ->falseLabel('Not verified users')
])
......

Select Filters

So the name itself illustrates the use of this filter. If we want the filter with dropdown then we can use this type of filters. The use of this type of filter is same as how we used to create a select form right. Check the code below for better understanding.

SelectFilter::make('status')
    ->options([
        'draft' => 'Draft',
        'reviewing' => 'Reviewing',
        'published' => 'Published',
    ])

We just import the SelectFilter and pass the attribute name there and pass the options for it. Want to learn more about Select Filter. Check this docs.

Query Builder

The use of this type of filter is to define a complex set of conditions to filter the data. To use this , we just need to add some constraints to it on the basis of our requirement.

QueryBuilder::make()
    ->constraints([
        TextConstraint::make('name'),
        BooleanConstraint::make('is_visible'),
        NumberConstraint::make('stock'),
        SelectConstraint::make('status')
            ->options([
                'draft' => 'Draft',
                'reviewing' => 'Reviewing',
                'published' => 'Published',
            ])
            ->multiple(),
        DateConstraint::make('created_at'),
        RelationshipConstraint::make('categories')
            ->multiple()
            ->selectable(
                IsRelatedToOperator::make()
                    ->titleAttribute('name')
                    ->searchable()
                    ->multiple(),
            ),
        NumberConstraint::make('reviewsRating')
            ->relationship('reviews', 'rating')
            ->integer(),
    ])

If you check the above filter , you can find out that we are using NumberConstraint, RelationshipConstraint , TextConstraint, BooleanConstraint that are predefined in Laravel Filament.I can’t cover everything about this filter here, so be sure to check the docs for more details!

Conclusion

So, in this way we can utilize the Laravel Filament Filters. If you have any queries or confusion , please comment down below.

1 thought on “Everything you need to know about Laravel Filament Filters”

  1. Pingback: How to add filters on dashboard page for widgets? - Code Daily

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top