> ## Documentation Index
> Fetch the complete documentation index at: https://filament-34a8cf01.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom filters

> [!WARNING]
>
> You are currently viewing the documentation for Filament 3.x, which is a previous version of Filament.
>
> Looking for the current stable version? Visit the [5.x documentation](https://filament-34a8cf01.mintlify.site/5.x/tables/filters/custom.md).

## Custom filter forms

![Filament](https://filament-34a8cf01.mintlify.site/docs/images/laracasts/filament-laracasts-logo.webp)
![Laracasts](https://filament-34a8cf01.mintlify.site/docs/images/laracasts/laracasts.svg)

### Build a Custom Table Filter

Watch the Build Advanced Components for Filament series on Laracasts - it will teach you how to build components, and you'll get to know all the internal tools to help you.

[Play Video](https://laracasts.com/series/build-advanced-components-for-filament/episodes/11?referer=DanHarrin)

![Laracasts](https://filament-34a8cf01.mintlify.site/docs/images/laracasts/laracasts.svg)

![Dan Harrin](https://avatars.githubusercontent.com/u/41773797?v=4)

Dan Harrin

Instructor

[https://twitter.com/danjharrin](https://twitter.com/danjharrin)

[https://danharrin.com](https://danharrin.com)

[https://github.com/danharrin](https://github.com/danharrin)

You may use components from the [Form Builder](https://filament-34a8cf01.mintlify.site/3.x/forms/fields/getting-started.md) to create custom filter forms. The data from the custom filter form is available in the `$data` array of the `query()` callback:

```php theme={"theme":"gruvbox-dark-hard"}
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

Filter::make('created_at')
    ->form([
        DatePicker::make('created_from'),
        DatePicker::make('created_until'),
    ])
    ->query(function (Builder $query, array $data): Builder {
        return $query
            ->when(
                $data['created_from'],
                fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
            )
            ->when(
                $data['created_until'],
                fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
            );
    })
```

Loading preview

![Table with custom filter form](https://filament-34a8cf01.mintlify.site/docs/images/3.x/light/tables/filters/custom-form.jpg)

### Setting default values for custom filter fields

To customize the default value of a field in a custom filter form, you may use the `default()` method:

```php theme={"theme":"gruvbox-dark-hard"}
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Filters\Filter;

Filter::make('created_at')
    ->form([
        DatePicker::make('created_from'),
        DatePicker::make('created_until')
            ->default(now()),
    ])
```

## Active indicators

When a filter is active, an indicator is displayed above the table content to signal that the table query has been scoped.

Loading preview

![Table with filter indicators](https://filament-34a8cf01.mintlify.site/docs/images/3.x/light/tables/filters/indicators.jpg)

By default, the label of the filter is used as the indicator. You can override this using the `indicator()` method:

```php theme={"theme":"gruvbox-dark-hard"}
use Filament\Tables\Filters\Filter;

Filter::make('is_admin')
    ->label('Administrators only?')
    ->indicator('Administrators')
```

If you are using a [custom filter form](#custom-filter-forms), you should use [`indicateUsing()`](#custom-active-indicators) to display an active indicator.

Please note: if you do not have an indicator for your filter, then the badge-count of how many filters are active in the table will not include that filter.

### Custom active indicators

Not all indicators are simple, so you may need to use `indicateUsing()` to customize which indicators should be shown at any time.

For example, if you have a custom date filter, you may create a custom indicator that formats the selected date:

```php theme={"theme":"gruvbox-dark-hard"}
use Carbon\Carbon;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Filters\Filter;

Filter::make('created_at')
    ->form([DatePicker::make('date')])
    // ...
    ->indicateUsing(function (array $data): ?string {
        if (! $data['date']) {
            return null;
        }

        return 'Created at ' . Carbon::parse($data['date'])->toFormattedDateString();
    })
```

### Multiple active indicators

You may even render multiple indicators at once, by returning an array of `Indicator` objects. If you have different fields associated with different indicators, you should set the field using the `removeField()` method on the `Indicator` object to ensure that the correct field is reset when the filter is removed:

```php theme={"theme":"gruvbox-dark-hard"}
use Carbon\Carbon;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\Indicator;

Filter::make('created_at')
    ->form([
        DatePicker::make('from'),
        DatePicker::make('until'),
    ])
    // ...
    ->indicateUsing(function (array $data): array {
        $indicators = [];

        if ($data['from'] ?? null) {
            $indicators[] = Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString())
                ->removeField('from');
        }

        if ($data['until'] ?? null) {
            $indicators[] = Indicator::make('Created until ' . Carbon::parse($data['until'])->toFormattedDateString())
                ->removeField('until');
        }

        return $indicators;
    })
```

### Preventing indicators from being removed

You can prevent users from removing an indicator using `removable(false)` on an `Indicator` object:

```php theme={"theme":"gruvbox-dark-hard"}
use Carbon\Carbon;
use Filament\Tables\Filters\Indicator;

Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString())
    ->removable(false)
```

[Edit this page on GitHub](https://github.com/filamentphp/filament/edit/3.x/packages/tables/docs/04-filters/05-custom.md)

## Sponsored by

[Your logo here](https://github.com/sponsors/danharrin)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.