apichef/laravel-request-query-helper

Easily interact with JSON-API query params.

v2.2.1 2021-03-28 18:57 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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.