Introduction | 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)  Introduction

 Version   v1      

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

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

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)

 Laravel Query Builder Go 
==========================

 [    Repository ](https://github.com/spatie/laravel-query-builder-go) 

    0 

    0 

Introduction
------------

###  On this page 

1. [ Basic usage ](#content-basic-usage)

This package allows you to filter, sort and include model relationships based on a request. It uses Laravel Go's native model builder, so you can start from an existing query and continue using native methods after calling `Build`. Query parameter names follow the [JSON API specification](https://jsonapi.org/) as closely as possible.

Basic usage
-----------------------------------------------------------------------------------------

The examples import `context`, `go.laravel.com/model` and `github.com/spatie/laravel-query-builder-go`. `User` is your application's model. Parse the HTTP request with `querybuilder.FromRequest(request)` and pass the result as `input`.

### Filter a query based on a request: `/users?filter[name]=John`:

```
func filteredUsers(ctx context.Context, input querybuilder.Request) ([]User, error) {
	return querybuilder.For(model.Query[User](), input).
		AllowedFilters(querybuilder.Partial[User]("name")).
		Get(ctx)
}
```

This fetches all users that contain the string "John" in their name. [Read more about filtering](features/filtering).

### Including relations based on a request: `/users?include=posts`:

```
func usersWithPosts(ctx context.Context, input querybuilder.Request) ([]User, error) {
	return querybuilder.For(model.Query[User](), input).
		IncludeRelations("posts").
		Get(ctx)
}
```

This fetches users with their posts loaded. [Read more about including relationships](features/including-relationships).

### Sorting a query based on a request: `/users?sort=id`:

```
func sortedUsers(ctx context.Context, input querybuilder.Request) ([]User, error) {
	return querybuilder.For(model.Query[User](), input).
		AllowedSorts(querybuilder.FieldSort[User]("id")).
		Get(ctx)
}
```

This sorts users by ascending ID. [Read more about sorting](features/sorting).

### Works together nicely with existing queries:

```
func activeUsers(ctx context.Context, input querybuilder.Request) ([]User, error) {
	subject := model.Query[User]().Where("active", "=", true).Where("score", ">", 42)
	return querybuilder.For(subject, input).
		Config(querybuilder.From(ctx)).
		IncludeRelations("posts", "permissions").
		Get(ctx)
}
```

`Config(querybuilder.From(ctx))` applies the settings registered with `querybuilder.New()`.

### Selecting fields for a query: `/users?fields=id,email`

```
type UserRow struct {
	ID    int64  `json:"id"`
	Email string `json:"email"`
}

func selectedUsers(ctx context.Context, input querybuilder.Request) ([]UserRow, error) {
	rows, err := querybuilder.For(model.Query[User](), input).
		AllowedFields("users", "id", "email").
		Select(ctx)
	if err != nil {
		return nil, err
	}
	return rows.Get[UserRow](ctx)
}
```

Unlike PHP, sparse selection returns row structs, not partially populated models. [Read more about selecting fields](features/selecting-fields).

 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)
