apichef / laravel-request-query-helper
Easily interact with JSON-API query params.
Requires
- php: ~7.4 || ^8.0
- illuminate/http: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-10-29 03:07:52 UTC
README
Easily interact with JSON-API query params.
Install
Via Composer
$ composer require apichef/laravel-request-query-helper
You can publish the config file with:
$ php artisan vendor:publish --provider="ApiChef\RequestQueryHelper\RequestQueryHelperServiceProvider"
Usage
Configuration
Default configuration is follows the {json:api} specification.
return [ /* Configuration for inclusion of related resources */ 'include' => [ 'name' => 'include', ], /* Configuration for filtering resource collections */ 'filter' => [ 'name' => 'filter', ], /* Configuration for sorting resource collections */ 'sort' => [ 'name' => 'sort', ], /* Configuration for sparse field-sets */ 'fields' => [ 'name' => 'fields', ], /* Configuration for pagination */ 'pagination' => [ 'name' => 'page', 'number' => 'number', 'size' => 'size', ], ];
Includes and Filters
This package adds filters
and includes
methods to the query object. Both methods returns QueryParamBag
, which is capable of parsing array-based and string-based request query strings.
Eg:
GET '/posts?include=comments:limit(5):sort(created_at|desc),author'
or
GET '/posts?include[comments][limit]=5&include[comments][sort]=created_at|desc&include[author]'
Following examples are based on above request.
use Illuminate\Http\Request; class ExampleController extends Controller { public function show(Request $request) { $params = $request->includes(); // has $params->has('comments'); // true $params->has('comments.limit'); // true $params->has('comments.sort'); // true $params->has('comments.foo'); // false $params->has('author'); // true // get $params->get('comments.limit'); // [5] $params->get('comments.sort'); // ['created_at', 'desc'] // isEmpty $params->isEmpty('comments'); // false $params->isEmpty('author'); // true } } ## Sorts // todo ## Fields // todo ## Pagination // todo
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email milroy@outlook.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.