Extending query builder | 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)  Advanced-usage  Extending query builder

 Version   v1      

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

  Extending query builder    
- [ 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)

 Extending query builder
=======================

###  On this page 

1. [ Handling validation errors ](#content-handling-validation-errors)
2. [ Application-owned models ](#content-application-owned-models)

- Pass an existing native query into `QueryBuilder::new`.
- Existing constraints, joins, named connections, limits and eager loads remain on it.
- Requested or default sorting replaces existing ordering.

```
let query = QueryBuilder::new(
    User::query().filter(User::active.eq(true)),
    QueryBuilderRequest::default(),
).build()?;

let users = query.filter(User::score.gt(50)).get().await?;
```

- Filter, sort and include callbacks take ownership of a native builder and return `Result<Builder<M>>`.
- Propagate validation failures as typed package errors and database failures as `Error::Framework`. Laravel's native error retains its `chain`, status and downcasting methods.
- Do not reconstruct the subject from its table name, which would discard its connection and application constraints.
- Use `builder.clone()` to branch a configured query. Changing one branch's allowlists or settings does not change the original. Native subject clauses and eager loads are retained.
- Models do not need to implement `Clone`. Callback captures are shared, so application-owned mutable callback state remains shared.
- Calling `build`, `get`, `count`, `first` or `paginate` rebuilds from the original subject and invokes callbacks again. These operations do not consume the configured builder.

Handling validation errors
--------------------------------------------------------------------------------------------------------------------------------------

Match the error variant instead of parsing its message:

```
use spatie_query_builder::{Error, QueryBuilderRequest};

let request = QueryBuilderRequest::from_query("filter[name");
assert!(matches!(request, Err(Error::UnclosedParameter)));
```

- `ExpectedRequestObject` rejects non-object JSON input.
- `MalformedParameter`, `UnclosedParameter`, `ParameterNestingTooDeep`, `InvalidArrayIndex` and `ArrayIndexTooLarge` describe URL parsing failures. Nesting is limited to 32 path components and array indexes to 10,000.
- `NotAllowed` contains the category and sorted, deduplicated unknown and allowed names.
- `ExpectedScalar`, `ExpectedText`, `InvalidDynamicOperator` and `OrderedNullComparison` describe built-in filter failures.
- `InvalidPagination` rejects zero values and overflowing offsets.
- `Json` propagates callback JSON conversion errors. `Framework` retains the native Laravel error.
- `Error` is non-exhaustive, so include a fallback arm when matching it.

Application-owned models
--------------------------------------------------------------------------------------------------------------------------------

- `QueryBuilder<M>` preserves the model type supplied by the application, including native `#[model(table = "accounts", connection = "reporting")]` settings, relation declarations and hooks.
- Pass that model's query directly to `QueryBuilder::new`. No package model configuration key or replacement model registry is needed.

 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)
