mphpmaster / laravel-builder-filter
Easily build Eloquent queries from API requests
Fund package maintenance!
hlack.net
Requires
- php: ^7.3|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/http: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- ext-json: *
- laravel/legacy-factories: ^1.0.4
- mockery/mockery: ^1.4
- orchestra/testbench: ^4.9|^5.8|^6.3
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-11-13 17:33:06 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 mPhpMaster\BuilderFilter\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
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
Appending attributes to a query: /users?append=full_name
$users = QueryBuilder::for(User::class) ->allowedAppends('full_name') ->get() ->toJson(); // the resulting JSON will have the `getFullNameAttribute` attributes included
Installation
You can install the package via composer:
composer require mphpmaster/laravel-builder-filter
Documentation
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 mPhpMaster@gmail.com 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 discover any security related issues, please email mphpmaster@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.