This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A minimal library for normalize Laravel app to return Json Api results.

v0.2.0-rc1 2019-04-09 13:44 UTC

This package is auto-updated.

Last update: 2021-10-09 20:00:16 UTC


composer require z1lab/laravel-json-api

Define .env keys:

CACHE_LIFETIME for set the default cache lifetime.

PAGINATION_SIZE for define the page size of collection results (Ex: GET index method).

PS: This package manipulate the cache using Cache::tags() so change the the CACHE_DRIVER to redis or memcache and install the required dephs. By default the predis/predis is included on the composer.json.


Laravel Json Api is a minimal library for normalize Laravel Apps to return Json Api results.

Before start take a look in the Json API Spec.

This package takes advantage of Laravel Eloquent Api Resources and some simple dependencies for improve the Json API pattern:

API Version Control

API Exception Handler

CORS Middleware

HTTP Cache

After starts, check the packages official docs for the default configuration of each one.


Just extends the required classes and apply the error handling.

Error handling

Change the render() method in the App\Exceptions\Handler:

use Z1lab\JsonApi\Exceptions\ApiHandler;

public function render($request, Exception $exception)
        if($request->wantsJson()) {
            $handler = new ApiHandler($this->container);

            return $handler->render($request, $exception);

        return parent::render($request, $exception);

API Controllers

In your controller class just extends the ApiController and set the constructor according the following example:

use Z1lab\JsonApi\Http\Controllers\ApiController;

class AwesomeController extends ApiController
    public function __construct(AwesomeRepository $repository)
        parent::__construct($repository, 'Awesome');

The fist parameter sets the repository for access the datasets; The second parameter defines the namespace to JsonResource class.

The ApiController extends all base methods from Laravel BaseController and delivery the initial methods for your ApiResourceController:

public function index() {}

public function show(string $id) {}

public function destroy(string $id) {}

public function makeResource($obj) {}

public function collectResource($collection) {}

You only have to implement the store() and update() methods with your FormRequest rules.

Api FormRequests

For optimal handle errors in yours FormRequests Just extends the ApiFormRequest class:

use Z1lab\JsonApi\Http\Requests\ApiFormRequest;

class AwesomeFormRequest extends ApiFormRequest {}

Api Repositories

In your repository class just extends the ApiRepository and set the constructor according the following example:

use Z1lab\JsonApi\Repositories\ApiRepository;

class AwesomeRepository extends ApiRepository
    public function __construct(Awesome $model)
        parent::__construct($model, 'awesome');

The fist parameter sets the model for access the data; The second parameter defines the cache prefix name.

The ApiRepository delivery some common methods for you handle your data accross the app.

public function create(array $data) {}

public function update(array $data, string $id) {}

public function destroy(string $id) {}

public function find(string $id, array $with = []) {}

public function list(int $items = 0) {}

public function findWhere(string $column, $value, array $with = []) {}

public function all(array $keys = []) {}


Laravel Json Api is open-sourced software licensed under the MIT license.