# Search filters

> Keep the rows that match conditions on fields, relation paths, pivot fields and related rows, with the operators that each field type accepts.

A filter reads its field through the field's declaration: the field decides whether it can be filtered, which operators it accepts and how its value is validated.

```php [app/Rest/Resources/PostResource.php]
use Illuminate\Contracts\Database\Eloquent\Builder;
use Lomkit\Rest\Fields\Boolean;
use Lomkit\Rest\Fields\Date;
use Lomkit\Rest\Fields\Enum;
use Lomkit\Rest\Fields\ID;
use Lomkit\Rest\Fields\Integer;
use Lomkit\Rest\Fields\Text;
use Lomkit\Rest\Relations\BelongsTo;
use Lomkit\Rest\Relations\BelongsToMany;
use Lomkit\Rest\Relations\HasMany;

public function fields(): array
{
    return [
        ID::make('id'),
        Text::make('title')->filterable(['=', 'like']),   // = and like only
        Text::make('summary')->nullable(),                 // the Text operators, plus is null and is not null
        Text::make('slug')->filterable(false),             // refused in filters
        Enum::make('status'),
        Boolean::make('featured'),
        Integer::make('views'),
        Date::make('published_on'),
    ];
}

public function relations(): array
{
    return [
        BelongsTo::make('author', AuthorResource::class),
        HasMany::make('comments', CommentResource::class),
        BelongsToMany::make('tags', TagResource::class)->pivotFields([
            Integer::make('weight'),
        ]),
    ];
}
```

## Conditions

```json [POST /api/posts/search]
{
  "search": {
    "filters": [
      {"field": "views", "operator": ">", "value": 100},
      {"field": "featured", "value": true}
    ]
  }
}
```

A filter holds `field`, `operator`, `value` and `type`:

- `field` names a filterable field of the resource, a relation path such as `author.last_name`, or a pivot field such as `tags.pivot.weight`.
- `operator` is one of the operators of the field. Without it, the filter compares with `=`.
- `value` is validated for the operator and the type of the field.
- `type` is `and`, the default, or `or`: an `or` filter is combined with the filter before it by `or`.

```json [POST /api/posts/search]
{
  "search": {
    "filters": [
      {"field": "status", "value": "published"},
      {"field": "featured", "value": true, "type": "or"}
    ]
  }
}
```

## Operators

The operators depend on the type of the field:

<table>
<thead>
  <tr>
    <th>
      Field
    </th>
    
    <th>
      Operators
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        ID
      </code>
      
      , <code>
        Enum
      </code>
    </td>
    
    <td>
      <code>
        =
      </code>
      
      , <code>
        !=
      </code>
      
      , <code>
        in
      </code>
      
      , <code>
        not in
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        Text
      </code>
    </td>
    
    <td>
      <code>
        =
      </code>
      
      , <code>
        !=
      </code>
      
      , <code>
        like
      </code>
      
      , <code>
        not like
      </code>
      
      , <code>
        ilike
      </code>
      
      , <code>
        not ilike
      </code>
      
      , <code>
        in
      </code>
      
      , <code>
        not in
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        Number
      </code>
      
      , <code>
        Integer
      </code>
    </td>
    
    <td>
      <code>
        =
      </code>
      
      , <code>
        !=
      </code>
      
      , <code>
        >
      </code>
      
      , <code>
        >=
      </code>
      
      , <code>
        <
      </code>
      
      , <code>
        <=
      </code>
      
      , <code>
        between
      </code>
      
      , <code>
        not between
      </code>
      
      , <code>
        in
      </code>
      
      , <code>
        not in
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        Boolean
      </code>
    </td>
    
    <td>
      <code>
        =
      </code>
      
      , <code>
        !=
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        Date
      </code>
      
      , <code>
        DateTime
      </code>
    </td>
    
    <td>
      <code>
        =
      </code>
      
      , <code>
        !=
      </code>
      
      , <code>
        >
      </code>
      
      , <code>
        >=
      </code>
      
      , <code>
        <
      </code>
      
      , <code>
        <=
      </code>
      
      , <code>
        between
      </code>
      
      , <code>
        not between
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        Json
      </code>
    </td>
    
    <td>
      none
    </td>
  </tr>
</tbody>
</table>

- A `nullable()` field adds `is null` and `is not null` to the operators of its type.
- `filterable([...])` narrows the operators of a field to the listed ones. Each one must be an operator of the field: `Boolean::make('featured')->filterable(['>'])` throws `InvalidFieldException`.
- On a field that does not accept `=`, such as `filterable(['like'])`, `operator` is required.
- The operators are the cases of `Lomkit\Rest\Fields\Filtering\FilterOperator`.

```json [POST /api/posts/search]
{
  "search": {
    "filters": [
      {"field": "title", "operator": "like", "value": "%Laravel%"},
      {"field": "views", "operator": "between", "value": [100, 500]},
      {"field": "status", "operator": "in", "value": ["published", "scheduled"]},
      {"field": "summary", "operator": "is not null"}
    ]
  }
}
```

### Values

<table>
<thead>
  <tr>
    <th>
      Operator
    </th>
    
    <th>
      Value
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        =
      </code>
      
      , <code>
        !=
      </code>
      
      , <code>
        >
      </code>
      
      , <code>
        >=
      </code>
      
      , <code>
        <
      </code>
      
      , <code>
        <=
      </code>
    </td>
    
    <td>
      A value of the field's type: a string for <code>
        Text
      </code>
      
      , an integer for <code>
        Integer
      </code>
      
      , a number for <code>
        Number
      </code>
      
      , a boolean for <code>
        Boolean
      </code>
      
      , a date for <code>
        Date
      </code>
      
       and <code>
        DateTime
      </code>
      
      , a value of the enum for <code>
        Enum
      </code>
      
      , a key of the model's key type for <code>
        ID
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        like
      </code>
      
      , <code>
        not like
      </code>
      
      , <code>
        ilike
      </code>
      
      , <code>
        not ilike
      </code>
    </td>
    
    <td>
      A string of at most <code>
        rest.limits.lengths.patterns
      </code>
      
       characters, 255 by default, or <code>
        rest.limits.lengths.values
      </code>
      
       when it is smaller
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        in
      </code>
      
      , <code>
        not in
      </code>
    </td>
    
    <td>
      A list of at least one value, at most <code>
        rest.limits.sizes.values
      </code>
      
       values (100 by default), each of the field's type
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        between
      </code>
      
      , <code>
        not between
      </code>
    </td>
    
    <td>
      A list of exactly two values of the field's type
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        is null
      </code>
      
      , <code>
        is not null
      </code>
    </td>
    
    <td>
      No value
    </td>
  </tr>
</tbody>
</table>

- `=` and `!=` refuse `null`. Use `is null` or `is not null` on a nullable field: `{"field": "summary", "operator": "is null"}`.
- A `Date` value is compared as the start of its day, and keeps the day as sent: `2026-10-12T23:30:00-05:00` is `2026-10-12`.
- A `DateTime` value with an offset is converted to `config('app.timezone')`, with every operator, `between` and `not between` included. With `UTC`, `{"field": "starts_at", "operator": ">=", "value": "2026-10-12T08:00:00+02:00"}` finds the rows from `2026-10-12 06:00:00`. A value without an offset is read in the application timezone. See [Dates and times](/resources/fields#dates-and-times).
- A `Date` or `DateTime` value is written in the date format of the model before the comparison. On SQLite, a `Date` filter compares the day of the column, whether it holds `2026-02-10` or `2026-02-10 00:00:00`.
- A string value, and each string of a list, has at most `rest.limits.lengths.values` characters, 1,000 by default, whatever the operator: `The search.filters.0.value field must not be greater than 1000 characters.`
- The error of an entry of a list goes under its position: `search.filters.0.value.1`.

### Case and databases

- `like` and `not like` follow the database: case-sensitive on PostgreSQL, the collation of the column on MySQL (case-insensitive on the default `_ci` collations), and the case of ASCII letters only is ignored on SQLite.
- `ilike` and `not ilike` ignore case. PostgreSQL runs `ilike`; MySQL and SQLite run `like`. On MySQL, `ilike` stays case-sensitive on a column with a binary or case-sensitive collation.

## Groups

`nested` groups filters, which then combine among themselves first. The group itself takes a `type`.

```json [POST /api/posts/search]
{
  "search": {
    "filters": [
      {"field": "status", "value": "published"},
      {
        "nested": [
          {"field": "views", "operator": ">", "value": 1000},
          {"field": "featured", "value": true, "type": "or"}
        ]
      }
    ]
  }
}
```

- A group holds `nested` and `type` only: `field`, `operator` or `value` next to `nested` give a 422.
- `rest.limits.depths.filters` sets how many `nested` levels a search accepts, 1 by default. A deeper group gives `The search.filters.0.nested.0.nested field is prohibited.`, and nothing below it is validated.
- Each `nested` list takes at most `rest.limits.sizes.filters` filters, like `filters` itself.

## Relation paths

A dotted `field` filters on a field of a related resource. It keeps the rows that have at least one related row matching the filter.

```json [POST /api/posts/search]
{
  "search": {
    "filters": [
      {"field": "author.last_name", "value": "Lovelace"},
      {"field": "comments.body", "operator": "like", "value": "%thanks%"}
    ]
  }
}
```

- The path is checked on the resource at its end: `author.last_name` needs a filterable `last_name` field on the resource of the `author` relation.
- The `perimeterQuery()` of the resource of each relation of the path applies, each in its own group: a post matches only through an author that the perimeter of the authors keeps.
- Each resource of the path needs `viewAny`, checked before validation. A denial answers `403`.
- A path goes through at most `rest.limits.depths.includes` relations, 3 by default: `The search.filters.0.field field goes through more relations than the limit of 3.`
- A field with `filterUsing()` runs its callback at the end of a path too.

### Pivot fields

A `pivot` segment reads a pivot field of a `BelongsToMany`, `MorphToMany` or `MorphedByMany` relation:

```json
{"field": "tags.pivot.weight", "operator": ">=", "value": 5}
```

- `pivot` is accepted only right after a many-to-many relation and right before the last segment.
- The last segment must be a filterable pivot field of that relation: a `writeOnly()` or `filterable(false)` pivot field, or a `selectable(false)` one without `filterable()`, gives a 422.
- The value is compared on the pivot table, morph pivot tables and the table of a pivot class included.

### MorphTo paths

A path through a `MorphTo` relation with one target, such as `commentable.title`, reads the field on the target resource and only matches the rows whose morph type is the model of that resource. Rows of another type never match. Through a `MorphTo` with several targets, a dotted path gives a 422: use a relation group with `types`.

## Relation groups

A filter that holds `relation` filters on the related rows of one relation, and applies all its filters to the same related row.

```json [POST /api/authors/search]
{
  "search": {
    "filters": [
      {
        "relation": "posts",
        "nested": [
          {"field": "status", "value": "published"},
          {"field": "featured", "value": true}
        ]
      },
      {"relation": "posts", "exists": false, "nested": [{"field": "status", "value": "draft"}], "type": "or"}
    ]
  }
}
```

The first group keeps the authors with one post that is both published and featured. Two dotted filters, `posts.status` and `posts.featured`, could match two different posts.

- The keys are `relation`, `exists`, `type` and `nested`, and `types` on a `MorphTo`.
- With `exists` `true`, the default, the group keeps the rows that have at least one related row matching every filter of `nested`. With `exists` `false`, it keeps the rows that have none.
- Without `nested`, or with `[]`, any related row counts: `{"relation": "posts", "exists": false}` keeps the authors without posts.
- `nested` takes the fields of the related resource, with their operators and values, `type`, groups, relation paths and relation groups. A relation group inside `nested` needs `rest.limits.depths.filters` at 2 or more.
- `relation` names one relation of the resource. A dotted path gives `The selected search.filters.0.relation is invalid.`: reach a deeper relation with a path in `nested`, such as `{"relation": "posts", "nested": [{"field": "comments.body", "value": "Thanks"}]}`.
- `exists` is `true` or `false`. Another value, `1`, `0` and `null` included, gives `The search.filters.0.exists field must be true or false.`
- `field`, `operator` or `value` next to `relation` give `The search.filters.0.field field is prohibited.`
- The `perimeterQuery()` of the related resource applies in its own group, and the related resource needs `viewAny`.
- On a many-to-many relation, `nested` takes the fields of the related resource. A pivot field there gives a 422: filter it with a path from the resource that declares the relation, `tags.pivot.weight`.

### MorphTo groups

On a `MorphTo`, the group takes `types`, one list of filters for each morph type:

```json
{"relation": "labelable", "types": {"book": [{"field": "title", "value": "Dune"}], "shelf": []}}
```

- The group keeps the rows whose target is a book titled Dune, or any shelf. An empty list means any target of that type.
- Without `types`, any target of a declared type counts.
- Each type applies its fields and its `perimeterQuery()`, and needs `viewAny`.
- With several targets, `nested` gives `The search.filters.0.nested field is prohibited: this relation has several types. Set it for each type, under types.` With one target, `nested` works, but not together with `types`.
- With `exists` `false`, the group keeps the rows of other types, of a type that is not a target, and without a type.

## Custom filters

`filterUsing()` filters a field with a callback instead of its column. A `virtual()` or `writeOnly()` field becomes filterable with it.

```php
use Illuminate\Contracts\Database\Eloquent\Builder;

Text::make('full_name')
    ->virtual()
    ->filterable(['=', 'like'])
    ->filterUsing(fn (Builder $query, string $operator, mixed $value) => $query
        ->where('first_name', $operator, $value)
        ->orWhere('last_name', $operator, $value)),
```

- The callback receives the Eloquent query, the operator as a string and the value as sent. It can also be an object that implements `Lomkit\Rest\Contracts\Filter`.
- Its conditions stay in their own group: an `orWhere()` in it does not leave the filter.
- The other fields are compared on their column, qualified with the table of the model.

## Fields that filters refuse

`filters` accepts the filterable fields only. These fields give `The selected search.filters.0.field is invalid.`:

- an undeclared field;
- a `Json` field;
- a field declared `filterable(false)`;
- a `virtual()` or `writeOnly()` field without `filterUsing()`;
- a field declared `selectable(false)` without `filterable()`.

## Limits

<table>
<thead>
  <tr>
    <th>
      Limit
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Counts
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        rest.limits.sizes.filters
      </code>
    </td>
    
    <td>
      100
    </td>
    
    <td>
      The entries of <code>
        filters
      </code>
      
      , of each <code>
        nested
      </code>
      
       list, and of the <code>
        filters
      </code>
      
       of includes and aggregates
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.totals.filters
      </code>
    </td>
    
    <td>
      500
    </td>
    
    <td>
      Every filter of one search, at every depth, in its includes and aggregates
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.totals.relations
      </code>
    </td>
    
    <td>
      20
    </td>
    
    <td>
      The relation filters of one search: each dotted path and each relation group runs a subquery
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.sizes.values
      </code>
    </td>
    
    <td>
      100
    </td>
    
    <td>
      The values of <code>
        in
      </code>
      
      , <code>
        not in
      </code>
      
      , <code>
        between
      </code>
      
       and <code>
        not between
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.totals.patterns
      </code>
    </td>
    
    <td>
      10
    </td>
    
    <td>
      The <code>
        like
      </code>
      
      , <code>
        not like
      </code>
      
      , <code>
        ilike
      </code>
      
       and <code>
        not ilike
      </code>
      
       filters of one search, at every depth, in its includes and aggregates
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.lengths.values
      </code>
    </td>
    
    <td>
      1000
    </td>
    
    <td>
      The characters of a string value, and of each string of a list
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.lengths.patterns
      </code>
    </td>
    
    <td>
      255
    </td>
    
    <td>
      The characters of a <code>
        like
      </code>
      
      , <code>
        not like
      </code>
      
      , <code>
        ilike
      </code>
      
       or <code>
        not ilike
      </code>
      
       value
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rest.limits.depths.filters
      </code>
    </td>
    
    <td>
      1
    </td>
    
    <td>
      The levels of <code>
        nested
      </code>
    </td>
  </tr>
</tbody>
</table>

A list over its limit gives a 422 on its own key, and nothing under it is validated:

```json [422 Unprocessable Content]
{
  "message": "The search.filters field goes over the limit of 20 relation filters in one search.",
  "errors": {
    "search.filters": ["The search.filters field goes over the limit of 20 relation filters in one search."]
  }
}
```

## Errors

<table>
<thead>
  <tr>
    <th>
      Message
    </th>
    
    <th>
      Key
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        The selected search.filters.0.field is invalid.
      </code>
    </td>
    
    <td>
      A field that filters refuse
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The selected search.filters.0.operator is invalid.
      </code>
    </td>
    
    <td>
      An operator that the field does not accept
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.operator field is required.
      </code>
    </td>
    
    <td>
      No operator on a field that does not accept <code>
        =
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.value field must be an integer.
      </code>
    </td>
    
    <td>
      A value of the wrong type, here on an <code>
        Integer
      </code>
      
       field
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.value field must contain 2 items.
      </code>
    </td>
    
    <td>
      <code>
        between
      </code>
      
       without exactly two values
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.value field must have at least 1 items.
      </code>
    </td>
    
    <td>
      <code>
        in
      </code>
      
       with an empty list
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.value field is prohibited.
      </code>
    </td>
    
    <td>
      A value next to <code>
        is null
      </code>
      
       or <code>
        is not null
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.value field must not be greater than 1000 characters.
      </code>
    </td>
    
    <td>
      A string value over <code>
        rest.limits.lengths.values
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters field goes over the limit of 10 pattern filters in one search.
      </code>
    </td>
    
    <td>
      More pattern filters than <code>
        rest.limits.totals.patterns
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.operater field is prohibited.
      </code>
    </td>
    
    <td>
      A key that a filter does not take
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        The search.filters.0.field field must be a string.
      </code>
    </td>
    
    <td>
      A <code>
        field
      </code>
      
       that is not a string
    </td>
  </tr>
</tbody>
</table>

In a search on a search engine, with a text or with `"source": "engine"`, filters follow the declaration of the engine instead, see [Search engines](/search/search-engines#filters-and-sorts).
