Easily build Eloquent queries from API requests

2.5.1 2020-01-22 13:18 UTC


Latest Version on Packagist GitHub Workflow Status StyleCI Quality Score Total Downloads

This package allows you to filter, sort and include eloquent relations based on a request. The QueryBuilder used in this package extends Laravel's default Eloquent builder. This means all your favorite methods and macros are still available. Query parameter names follow the JSON API specification as closely as possible.

Basic usage

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

use Spatie\QueryBuilder\QueryBuilder;

$users = QueryBuilder::for(User::class)

// all `User`s that contain the string "John" in their name

Read more about filtering features like: partial filters, exact filters, scope filters, custom filters, ignored values, default filter values, ...

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

$users = QueryBuilder::for(User::class)

// all `User`s with their `posts` loaded

Read more about include features like: including nested relationships, including relationship count, ...

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

$users = QueryBuilder::for(User::class)

// all `User`s sorted by ascending id

Read more about sorting features like: custom sorts, sort direction, ...

Works together nicely with existing queries:

$query = User::where('active', true);

$userQuery = QueryBuilder::for($query) // start from an existing Builder instance
    ->withTrashed() // use your existing scopes
    ->allowedIncludes('posts', 'permissions')
    ->where('score', '>', 42); // chain on any of Laravel's query builder methods

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

$users = QueryBuilder::for(User::class)
    ->allowedFields(['id', 'email'])

// the fetched `User`s will only have their id & email set

Read more about selecting fields.

Appending attributes to a query: /users?append=full_name

$users = QueryBuilder::for(User::class)

// the resulting JSON will have the `getFullNameAttribute` attributes included

Read more about appending attributes.


You can install the package via composer:

composer require spatie/laravel-query-builder

Read the installation notes on the docs site:


You can find the documentation on

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the media library? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

If you've found a bug regarding security please mail instead of using the issue tracker.

Upgrading from v1 to v2

Please see for details.


composer test


Please see CHANGELOG for more information what has changed recently.


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.


You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.


Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.


The MIT License (MIT). Please see License File for more information.