spatie / laravel-query-builder
Easily build Eloquent queries from API requests
Fund package maintenance!
spatie.be/open-source/support-us
Installs: 15 299 494
Dependents: 193
Suggesters: 7
Security: 0
Stars: 4 086
Watchers: 48
Forks: 401
Open Issues: 5
Requires
- php: ^8.2
- illuminate/database: ^10.0|^11.0
- illuminate/http: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.11
Requires (Dev)
- ext-json: *
- larastan/larastan: ^2.9
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^2.0
- phpunit/phpunit: ^10.0
- spatie/invade: ^2.0
- dev-main
- 6.2.2
- 6.2.1
- 6.2.0
- 6.1.0
- 6.0.1
- 6.0.0
- v5.x-dev
- 5.8.1
- 5.8.0
- 5.7.0
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- v4.x-dev
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.0.0
- v2.x-dev
- 2.8.4
- 2.8.3
- 2.8.2
- 2.8.0
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- v1.x-dev
- 1.18.0
- 1.17.5
- 1.17.4
- 1.17.3
- 1.17.2
- 1.17.1
- 1.17.0
- 1.16.1
- 1.16.0
- 1.15.2
- 1.15.1
- 1.15.0
- 1.14.0
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.4
- 1.10.3
- 1.10.1
- 1.10.0
- 1.9.6
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.0.3
- 0.0.2
- 0.0.1
- dev-AlexVanderbist/add-operator-filter-docs
- dev-phpstan
- dev-shift-53871
This package is auto-updated.
Last update: 2024-12-11 09:55:54 UTC
README
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) ->allowedFilters('name') ->get(); // all `User`s that contain the string "John" in their name
Including relations based on a request: /users?include=posts
:
$users = QueryBuilder::for(User::class) ->allowedIncludes('posts') ->get(); // all `User`s with their `posts` loaded
Sorting a query based on a request: /users?sort=id
:
$users = QueryBuilder::for(User::class) ->allowedSorts('id') ->get(); // 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[users]=id,email
$users = QueryBuilder::for(User::class) ->allowedFields(['id', 'email']) ->get(); // the fetched `User`s will only have their id & email set
Read more about selecting fields.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-query-builder
Read the installation notes on the docs site: https://spatie.be/docs/laravel-query-builder/v5/installation-setup.
Documentation
You can find the documentation on https://spatie.be/docs/laravel-query-builder/v5.
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 security@spatie.be instead of using the issue tracker.
Upgrading
Please see UPGRADING.md for details.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.