https://github.com/spatie/laravel-query-builder
Easily build Eloquent queries from API requests
https://github.com/spatie/laravel-query-builder
api hacktoberfest laravel php
Last synced: 1 day ago
JSON representation
Easily build Eloquent queries from API requests
- Host: GitHub
- URL: https://github.com/spatie/laravel-query-builder
- Owner: spatie
- License: mit
- Created: 2018-01-15T16:14:37.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-05-03T13:29:38.000Z (6 days ago)
- Last Synced: 2025-05-08T17:59:23.355Z (1 day ago)
- Topics: api, hacktoberfest, laravel, php
- Language: PHP
- Homepage: https://spatie.be/docs/laravel-query-builder
- Size: 1.2 MB
- Stars: 4,206
- Watchers: 47
- Forks: 406
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Support: docs/support-us.md
Awesome Lists containing this project
- awesome-laravel-filament - Laravel Eloquent Query Builder
README
![]()
Build Eloquent queries from API requests
[](https://packagist.org/packages/spatie/laravel-query-builder)


[](https://packagist.org/packages/spatie/laravel-query-builder)
## Basic usage
### Filter a query based on a request: `/users?filter[name]=John`:
```php
use Spatie\QueryBuilder\QueryBuilder;$users = QueryBuilder::for(User::class)
->allowedFilters('name')
->get();// 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, ...](https://spatie.be/docs/laravel-query-builder/v5/features/filtering/)
### Including relations based on a request: `/users?include=posts`:
```php
$users = QueryBuilder::for(User::class)
->allowedIncludes('posts')
->get();// all `User`s with their `posts` loaded
```[Read more about include features like: including nested relationships, including relationship count, custom includes, ...](https://spatie.be/docs/laravel-query-builder/v5/features/including-relationships/)
### Sorting a query based on a request: `/users?sort=id`:
```php
$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, ...](https://spatie.be/docs/laravel-query-builder/v5/features/sorting/)
### Works together nicely with existing queries:
```php
$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`
```php
$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.](https://spatie.be/docs/laravel-query-builder/v5/features/selecting-fields/)
## Support us
[
](https://spatie.be/github-ad-click/laravel-query-builder)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
## Installation
You can install the package via composer:
```bash
composer require spatie/laravel-query-builder
```Read the installation notes on the docs site: [https://spatie.be/docs/laravel-query-builder/v5/installation-setup](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](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](https://github.com/spatie/laravel-query-builder/issues), we'll try to address it as soon as possible.
If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.
### Upgrading
Please see [UPGRADING.md](UPGRADING.md) for details.
### Testing
```bash
composer test
```### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
### Security
If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.
## Credits
- [Alex Vanderbist](https://github.com/AlexVanderbist)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.