Extending query builder | laravel-query-builder-go | Spatie       

 SPATIE  

laravel-query-builder-go
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-query-builder-go](https://spatie.be/docs/laravel-query-builder-go/v1)  Advanced-usage  Extending query builder

 Version   v1      

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

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

Features
--------

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

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

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

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

You can specify a base query instead of a model name, then return to the native builder for any additional methods:

```
query, err := querybuilder.For(model.Query[User]().Where("id", "=", 42), input).
    IncludeRelations("posts").
    Build(r)
if err != nil {
    return web.Fail(err)
}
user, err := query.Where("active", "=", true).First(r)
```

`For` also accepts a typed `model.Related` query. Your model type, table overrides and native connection selection remain on model reads. No package-owned model is required. Replace the generic type to use another model, including its custom `db` tags and relationship definitions.

Builder methods return values, not mutable references. Assigning a builder creates a branch. Calling `Build` or a read method repeatedly starts from the original subject each time. Captured callback state is shared, so synchronize it if the same builder is used concurrently.

Use `CallbackFilter`, `CustomSort` and `CustomInclude` to inject functions or method values. You may capture your own configurable profile implementation. Callback errors propagate unchanged. Perform fallible lookup work in `CallbackFilter`, where the context and an error return are available. Keep deferred native relationship callbacks free of side effects.

 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)
