Skip to content

Filtering

Types of Filters

There are several types of filters that can be added to the Table:

  • TextFilter
  • NumericFilter
  • SetFilter
  • DateFilter
  • BooleanFilter

Adding Filters

To add filters to your table, you can use the make method on the filter class and pass the attribute of the Eloquent Model you want to filter. Add the filter to the array returned by the filters() method in your Table class.

php
TextFilter::make('name');

It will automatically generate a title-cased header based on the attribute name. If you want to customize the header, you can pass the header as the second argument.

php
TextFilter::make('name', 'Full Name');

Clauses

Each filter has one or more clauses that define how the filter should be applied. The clauses are defined in the Clause enum and each type of filter has its own set of default clauses. If you want to customize the available clauses, you can pass an array of clauses to the clauses parameter, or by using the clauses() method.

php
use InertiaUI\Table\Filters\Clause;

TextFilter::make('name')->clauses([
    Clause::Equals,
    Clause::StartsWith,
    Clause::Contains,
]);

Nullable attributes

If you want to filter nullable attributes, you may pass the nullable argument to the filter to add the IsSet and IsNotSet clauses. Alternatively, you can use the nullable() method.

php
TextFilter::make('name', nullable: true);

TextFilter::make('name')->nullable();

Validate and Modify Input

By default, the incoming filter input is validated to be filled and to be of the correct type. If you want to customize the validation process, you can pass a closure to the validateUsing parameter, or call the validateUsing() method.

php
TextFilter::make('name')->validateUsing(function (mixed $value) {
    if(is_string($value) && strlen($value) > 3) {
        return $value;
    }
});

Note that the callback should return the value if it passes the validation, not a boolean. This way you can modify the value before it is used in the query.

The callback accepts two additional parameters: the clause and the Eloquent builder:

php
use Illuminate\Database\Eloquent\Builder;
use InertiaUI\Table\Filters\Clause;

TextFilter::make('name')->validateUsing(function (mixed $value, Clause $clause, Builder $query) {
    //
});

Customizing the Query

Each combination of filter and clause has a specific way of applying the filter to the query. If you want to customize this, you can pass a closure to the applyUsing parameter, or call the applyUsing() method.

php
use Illuminate\Database\Eloquent\Builder;
use InertiaUI\Table\Filters\Clause;

TextFilter::make('name')->applyUsing(function (Builder $resource, string $attribute, Clause $clause, mixed $value) {
    if($clause === Clause::Equals) {
        $resource->where($attribute, $value);
    }
});

You might wonder why you want to pass such an extensive callback to the static make() method, but remember that you may also pass invokable classes:

php
TextFilter::make('name', applyUsing: new MyCustomTextFilter);

Set Filter

The SetFilter has some additional options. You can pass an array of options to the options parameter, or call the options() method to populate the select options.

php
SetFilter::make('status', options: [
    'active' => 'Active',
    'inactive' => 'Inactive',
]);

If you want to allow multiple selections, you can pass the multiple argument to the filter, or call the multiple() method.

php
SetFilter::make('tags')->multiple();

Filtering by Relationships

The SetFilter is great for filtering by relationships. It comes with a pluckOptionsFromModel helper method that allows you to easily populate the options from an Eloquent Model.

php
SetFilter::make('company.id', 'Company')->pluckOptionsFromModel(Company::class, 'name');