Filtering | laravel-query-builder-rust | Spatie       

 SPATIE  

laravel-query-builder-rust
==========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-query-builder-rust](https://spatie.be/docs/laravel-query-builder-rust/v1)  Features  Filtering

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-query-builder-rust/v1) 

  Filtering    
- [ Introduction ](https://spatie.be/docs/laravel-query-builder-rust/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-query-builder-rust/v1/requirements)
- [ About us ](https://spatie.be/docs/laravel-query-builder-rust/v1/about-us)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-query-builder-rust/v1/installation-setup)
- [ Support us ](https://spatie.be/docs/laravel-query-builder-rust/v1/support-us)
- [ Questions and issues ](https://spatie.be/docs/laravel-query-builder-rust/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-query-builder-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-query-builder-rust/v1/testing)

Features
--------

- [ Filtering ](https://spatie.be/docs/laravel-query-builder-rust/v1/features/filtering)
- [ Sorting ](https://spatie.be/docs/laravel-query-builder-rust/v1/features/sorting)
- [ Including relationships ](https://spatie.be/docs/laravel-query-builder-rust/v1/features/including-relationships)
- [ Selecting fields ](https://spatie.be/docs/laravel-query-builder-rust/v1/features/selecting-fields)

Advanced usage
--------------

- [ Extending query builder ](https://spatie.be/docs/laravel-query-builder-rust/v1/advanced-usage/extending-query-builder)
- [ Pagination ](https://spatie.be/docs/laravel-query-builder-rust/v1/advanced-usage/pagination)
- [ Multi value delimiter ](https://spatie.be/docs/laravel-query-builder-rust/v1/advanced-usage/multi-value-delimiter)
- [ Front-end implementation ](https://spatie.be/docs/laravel-query-builder-rust/v1/advanced-usage/front-end-implementation)

 Filtering
=========

###  On this page 

1. [ Filter types ](#content-filter-types)
2. [ Custom filters, scopes and groups ](#content-custom-filters-scopes-and-groups)

- Register filters with `allowed_filters`.
- Unknown names return `Error::NotAllowed` before application callbacks run.
- Different filters combine with AND.

```
let request = QueryBuilderRequest::from_query("filter[name]=John&filter[score]=>=40,<90")?;
let users = QueryBuilder::new(User::query(), request)
    .allowed_filters([
        AllowedFilter::partial("name", User::name),
        AllowedFilter::operator("score", FilterOperator::Dynamic),
    ])
    .get().await?;
```

Filter types
--------------------------------------------------------------------------------------------

- `partial(name, column)`, `begins_with(name, column)` and `ends_with(name, column)` take a native `Column<Model, String>`. Laravel's `contains`, `starts_with` and `ends_with` supply literal `%`, `_` and backslash handling. Case sensitivity follows Laravel's database behavior; SQLite ignores ASCII case only.
- `exact(name)` uses equality for scalar values and OR for lists. `.internal_name("users.name")` maps the public exact/operator name to a native SQL column. Text filters already name their typed column, so no internal-name override is needed for them.
- `operator(name, operator)` accepts `Equal`, `NotEqual`, `LessThan`, `GreaterThan`, `LessThanOrEqual`, `GreaterThanOrEqual`, or `Dynamic`. Dynamic strings recognize `<=`, `>=`, `<>`, `<`, `>`, `=`. Operator lists combine with AND; `.or_values()` uses OR. Ordered null comparisons are errors.
- `.default(value)` applies only when the filter is absent. `.unset_default()` removes it without changing explicit request handling or nullability.
- `.ignore(values)` accumulates ignored values across calls and removes them recursively from lists and objects.
- Values use JSON equality after normalizing `"true"` and `"false"` into booleans. Other scalar types are not coerced.
- Callbacks retain nested objects, object keys and list shape, even if only one list item survives. Entirely ignored or empty collections apply no filter.
- `.nullable()` converts empty strings into null values; otherwise explicit null values are ignored. `.default(null)` also enables nullable behavior.
- Exact and equality/inequality operator filters turn null values into SQL null comparisons. Ordered null comparisons return `Error::OrderedNullComparison`.
- Text filters require non-null scalar values; use a callback for nullable text behavior. Text lists skip empty and malformed members rather than broadening the search.
- Filter lists split on commas unless configuration or `.delimiter(...)` changes it. Boolean strings are normalized after splitting too, so `filter[active]=true,false` matches either boolean value.

Custom filters, scopes and groups
---------------------------------------------------------------------------------------------------------------------------------------------------------

- Callbacks receive a native builder and the normalized JSON value, returning `Result<Builder<Model>>`.
- Parse and validate application-specific values in the callback. JSON conversion errors propagate through `Error::Json` using `?`.
- Compose native conditions using `&` and `|`. An application scope can be an ordinary function that accepts and returns the native builder.

For example, a callback can use `query.has_where(User::posts, Post::title.eq(title))` for a related title, or `query.filter(User::name.contains(text) | User::nickname.contains(text))` for an OR group. Related names are explicitly registered; dotted strings never automatically resolve model methods. See [relationships](including-relationships).

Laravel 0.1.0 has no ORM soft deletes. There is no built-in `trashed` filter or automatic deleted-row exclusion.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
