# Nova - Filters/Defining-Filters

*Source: https://nova.laravel.com/docs/v5/filters/defining-filters*

---

[Laravel Nova home page](https://nova.laravel.com)
v5
Search...
⌘KAsk AI
- [email protected]
- [Platform Status](https://status.laravel.com/)
- [Dashboard](https://nova.laravel.com)
- [Dashboard](https://nova.laravel.com)
Search...
Navigation
Filters
Defining Filters
[Documentation](/docs/v5/installation)[Knowledge Base](/docs/kb/support)
- [Blog](https://blog.laravel.com)
##### Get Started
- [Installation](/docs/v5/installation)
- [Release Notes](/docs/v5/releases)
- [Upgrade Guide](/docs/v5/upgrade)
##### Resources
- [The Basics](/docs/v5/resources/the-basics)
- [Fields](/docs/v5/resources/fields)
- [Dependent Fields](/docs/v5/resources/dependent-fields)
- [Date Fields](/docs/v5/resources/date-fields)
- [File Fields](/docs/v5/resources/file-fields)
- [Repeater Fields](/docs/v5/resources/repeater-fields)
- [Field Panels](/docs/v5/resources/panels)
- [Relationships](/docs/v5/resources/relationships)
- [Validation](/docs/v5/resources/validation)
- [Authorization](/docs/v5/resources/authorization)
##### Search
- [The Basics](/docs/v5/search/the-basics)
- [Global Search](/docs/v5/search/global-search)
- [Scout Integration](/docs/v5/search/scout-integration)
##### Filters
- [Defining Filters](/docs/v5/filters/defining-filters)
- [Registering Filters](/docs/v5/filters/registering-filters)
##### Lenses
- [Defining Lenses](/docs/v5/lenses/defining-lenses)
- [Registering Lenses](/docs/v5/lenses/registering-lenses)
##### Actions
- [Defining Actions](/docs/v5/actions/defining-actions)
- [Registering Actions](/docs/v5/actions/registering-actions)
##### Metrics
- [Defining Metrics](/docs/v5/metrics/defining-metrics)
- [Registering Metrics](/docs/v5/metrics/registering-metrics)
##### Digging Deeper
- [Dashboards](/docs/v5/customization/dashboards)
- [Menus](/docs/v5/customization/menus)
- [Notifications](/docs/v5/customization/notifications)
- [Authentication](/docs/v5/customization/authentication)
- [Impersonation](/docs/v5/customization/impersonation)
- [Tools](/docs/v5/customization/tools)
- [Resource Tools](/docs/v5/customization/resource-tools)
- [Cards](/docs/v5/customization/cards)
- [Fields](/docs/v5/customization/fields)
- [Filters](/docs/v5/customization/filters)
- [CSS / JavaScript](/docs/v5/customization/frontend)
- [Assets](/docs/v5/customization/assets)
- [Localization](/docs/v5/customization/localization)
- [Stubs](/docs/v5/customization/stubs)
## On this page
- [Select Filters](#select-filters)
- [Boolean Filters](#boolean-filters)
- [Date Filters](#date-filters)
- [Filter Titles](#filter-titles)
- [Filter Default Values](#filter-default-values)
- [Dynamic Filters](#dynamic-filters)
Filters
# Defining Filters
Nova filters are simple classes that allow you to scope your Nova index queries with custom conditions.
> ## Documentation Index
>
> Fetch the complete documentation index at: <https://nova.laravel.com/docs/llms.txt>
>
> Use this file to discover all available pages before exploring further.
Nova filters are simple classes that allow you to scope your Nova index queries with custom conditions.
Before creating your own filters, you may want to check out [filterable fields](./../resources/fields#filterable-fields). Filterable fields can solve the filtering needs of most Nova installations without the need to write custom code.
## [​](#select-filters) Select Filters
The most common type of Nova filter is the “select” filter, which allows the user to select a filter option from a drop-down selection menu:
You may generate a select filter using the `nova:filter` Artisan command. By default, Nova will place newly generated filters in the `app/Nova/Filters` directory:
```
php artisan nova:filter UserType
```
Each select filter generated by Nova contains two methods: `apply` and `options`. The `apply` method is responsible for modifying the underlying Eloquent query to achieve the desired filter state, while the `options` method defines the “values” the filter may have. Let’s take a look at an example `UserType` filter:
app/Nova/Filters/UserType.php
```
namespace App\Nova\Filters;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Laravel\Nova\Filters\Filter;
use Laravel\Nova\Http\Requests\NovaRequest;

class UserType extends Filter
{
    /**
     * The filter's component.
     *
     * @var string
     */
    public $component = 'select-filter';

    /**
     * Apply the filter to the given query.
     */
    public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
    {
        return $query->where('type', $value);
    }

    /**
     * Get the filter's available options.
     *
     * @return array<string, string>
     */
    public function options(NovaRequest $request): array
    {
        return [
            'Administrator' => 'admin',
            'Editor' => 'editor',
        ];
    }
}
```
The `options` method should return an array of keys and values. The array’s keys will be used as the “human-friendly” text that will be displayed in the Nova UI, while the array’s values will be passed into the `apply` method as the `$value` argument. This filter defines two possible values: `admin` and `editor`.
As you can see in the example above, you may leverage the incoming `$value` to modify your query, and the `apply` method should return the modified query instance.
## [​](#boolean-filters) Boolean Filters
Nova also supports “boolean” filters, which allow the user to select multiple filter options via a list of check-boxes:
You may generate a boolean filter using the `nova:filter --boolean` Artisan command. By default, Nova will place newly generated filters in the `app/Nova/Filters` directory:
```
php artisan nova:filter UserType --boolean
```
Each boolean filter generated by Nova contains two methods: `apply` and `options`. The `apply` method is responsible for modifying the Eloquent query to achieve the desired filter state, while the options method defines the “values” the filter may have.
When building boolean filters, the `$value` argument passed to the `apply` method is an associative array containing the boolean value of each of your filter’s options. Let’s take a look at an example `UserType` filter:
app/Nova/Filters/UserType.php
```
use Illuminate\Contracts\Database\Eloquent\Builder;
use Laravel\Nova\Http\Requests\NovaRequest;

// ...

/**
 * Apply the filter to the given query.
 */
public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
{
    /** @var array{admin: bool, editor: bool} $value */
    return $query->where(function ($query) use ($value) {
        return $query->where('is_admin', $value['admin'])
                 ->where('is_editor', $value['editor']);
    });
}
```
The `options` method should return an array of keys and values. The array’s keys will be used as the “human-friendly” text that will be displayed in the Nova UI. The array’s values will be passed into the `apply` method as the `$value` argument. This filter defines two possible values: `admin` and `editor`.
As you can see in the example above, you may leverage the incoming `$value` to modify your query. The `apply` method should return the modified query instance.
## [​](#date-filters) Date Filters
Nova also supports “date” filters, which allow the user to select the filter’s value via a date selection calendar:
You may generate a date filter using the `nova:filter --date` Artisan command. By default, Nova will place newly generated filters in the `app/Nova/Filters` directory:
```
php artisan nova:filter BirthdayFilter --date
```
Each date filter generated by Nova contains one method: `apply`. The `apply` method is responsible for modifying the query to achieve the desired filter state.
When building date filters, the `$value` argument passed to the `apply` method is the string representation of the selected date. Let’s take a look at an example `BirthdayFilter` filter:
app/Nova/Filters/BirthdayFilter.php
```
namespace App\Nova\Filters;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Laravel\Nova\Filters\DateFilter;
use Laravel\Nova\Http\Requests\NovaRequest;

class BirthdayFilter extends DateFilter
{
    /**
     * Apply the filter to the given query.
     */
    public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
    {
        return $query->where('birthday', '<=', Carbon::parse($value));
    }
}
```
As you can see in the example above, you may leverage the incoming `$value` to modify your query. The `apply` method should return the modified query instance.
## [​](#filter-titles) Filter Titles
If you would like to change the filter title that is displayed in Nova’s filter selection menu, you may define a `name` property on the filter class:
app/Nova/Filters/UserType.php
```
/**
 * The displayable name of the filter.
 *
 * @var \Stringable|string
 */
public $name = 'Filter by User Type';
```
If the name of your filter needs to be dynamic, you should create a `name` method on the filter class:
app/Nova/Filters/UserType.php
```
/**
 * Get the displayable name of the filter.
 *
 * @return \Stringable|string
 */
public function name()
{
    return 'Filter By '.$this->customProperty;
}
```
## [​](#filter-default-values) Filter Default Values
If you would like to set the default value of a filter, you may define a `default` method on the filter class:
app/Nova/Filters/UserType.php
```
/**
 * The default value of the filter.
 *
 * @var array|mixed
 */
public function default()
{
    return [
        'admin' => true,
        'editor' => false,
    ];
}
```
## [​](#dynamic-filters) Dynamic Filters
There may be times when you want to create a dynamic filter which filters on columns that are determined at runtime, allowing you to reuse a filter class across multiple different resources and fields.
To accomplish this, you could pass the name of the filterable column into the filter’s constructor. In addition to passing the column name that we want to filter on in the constructor, we’ll also need to override the `key` method of the filter so that Nova can uniquely identify this instance of the filter if multiple instances of this filter exist on the page. Let’s take a look at an example `TimestampFilter` filter:
app/Nova/Filters/TimestampFilter.php
```
namespace App\Nova\Filters;

use Illuminate\Support\Carbon;
use Laravel\Nova\Filters\DateFilter;
use Laravel\Nova\Http\Requests\NovaRequest;

class TimestampFilter extends DateFilter
{
    /**
     * Create a new filter instance.
     */
    public function __construct(
        protected string $column
    ) { }

    /**
     * Apply the filter to the given query.
     */
    public function apply(NovaRequest $request, Builder $query, mixed $value): Builder
    {
        return $query->where($this->column, '<=', Carbon::parse($value));
    }

    /**
     * Get the key for the filter.
     */
    public function key(): string
    {
        return 'timestamp_'.$this->column;
    }
}
```
Then, as discussed, you should pass the name of the column you wish to filter on:
app/Nova/~Resource.php
```
use Laravel\Nova\Http\Requests\NovaRequest;

// ...

/**
 * Get the filters available for the resource.
 *
 * @return array<int, \Laravel\Nova\Filters\Filter>
 */
public function filters(NovaRequest $request): array
{
    return [
        new Filters\TimestampFilter('created_at'),
        new Filters\TimestampFilter('deleted_at'),
    ];
}
```
Was this page helpful?
YesNo
[Scout Integration](/docs/v5/search/scout-integration)[Registering Filters](/docs/v5/filters/registering-filters)
⌘I
[Laravel Nova home page](https://nova.laravel.com)
Platform
[Dashboard](https://nova.laravel.com/)[Status](https://status.laravel.com/)
Legal and Compliance
[Term of Service](https://nova.laravel.com/terms)[Privacy Policy](https://nova.laravel.com/privacy)
Assistant
Responses are generated using AI and may contain mistakes.