PHP code example of sidigi / laravel-json-api-request

1. Go to this page and download the library: Download sidigi/laravel-json-api-request library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sidigi / laravel-json-api-request example snippets


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

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

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->