Via Composer
$ composer require apichef/laravel-request-query-helper
You can publish the config file with:
$ php artisan vendor:publish --provider="ApiChef\RequestQueryHelper\RequestQueryHelperServiceProvider"
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',
],
];
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
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.