nadlambino / laravel-query-builder
This is a forked of spatie/laravel-query-builder but decoupled the builder from request.
Fund package maintenance!
spatie.be/open-source/support-us
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: *
- mockery/mockery: ^1.4
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^2.0
- phpunit/phpunit: ^10.0
- spatie/invade: ^2.0
README
The goal is to allow the QueryBuilder
class to not be tightly coupled from the request object. The package should be able to use outside of the context of API request, write a single query builder that can be use with API and in any other part of the application where the filters are not from the request.
What are the changes?
- First, the package namespace was changed to avoid conflict and confusion from the
spatie/laravel-query-builder
. - Renamed the
QueryBuilderRequest
toRequestSource
and move it into thesources
directory. The namespace were also changed due to moving it to its new location. - Created a
CollectionSource
which shares the same methods from theRequestSource
viaSource
trait. The only difference is how thegetData
(previouslygetRequestData
) was implemented. - Changing the delimeter can now be done through the source class. Previously, it can be done using the
QueryBuilderRequest
/** Previously */ QueryBuilderRequest::setFilterArrayValueDelimiter('|'); /** Now, when using the request as the source */ RequestSource::setFilterArrayValueDelimiter('|'); /** Or when using the array/collection as the source */ CollectionSource::setFilterArrayValueDelimiter('|');
Build Eloquent queries from API requests
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.