sidigi/laravel-json-api-request

2.0.1 2021-03-18 07:31 UTC

This package is auto-updated.

Last update: 2024-11-18 15:34:26 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Provide some JSON API validation stuff JSON API for laravel Form requests.

Installation

You can install the package via composer:

composer require sidigi/laravel-json-api-request

Usage

Controller

    public function index(UserIndexRequest $request)
    {
        $users = User::all();

        return response()->json($users);
    }

Form Request

use Illuminate\Foundation\Http\FormRequest;
use Sidigi\LaravelJsonApiRequest\Traits;

class UserIndexRequest extends FormRequest
{
    use IsJsonApiRequest;

    public function rules()
    {
        return $this->jsonApiRules()
    }

    //filter value ?filter[id]=1,2,3 - for all values
    public function valueFilterRules()
    {
        return [
            'id' => 'exists:users,id'
        ]
    }

    //filter value ?filter[id]=1,2,3 - for each values
    public function eachValueFilterRules()
    {
        return [
            'id' => 'integer'
        ]
    }
}
use Illuminate\Foundation\Http\FormRequest;
use Sidigi\LaravelJsonApiRequest\Traits;

class UserIndexRequest extends FormRequest
{
    use HasFilterField,
        HasGroupFields,
        HasIncludeFields,
        HasBasePaginationFields,
        HasSortFields;

    public function rules()
    {
        return array_merge(
            $this->sortRules(), // HasSortFields
            $this->filterRules(), //HasFilterField
            $this->groupRules(), //HasGroupFields
            $this->includeRules(), //HasIncludeFields
            $this->paginationRules() //HasBasePaginationFields
        );
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.