Introduction
Installing and configuring Laravel Apiable into your Laravel application.
Install with the following command:
composer require open-southeners/laravel-apiableGetting started
First publish the config file once installed like this:
php artisan vendor:publish --provider="OpenSoutheners\LaravelApiable\ServiceProvider"Then edit the resource_type_map part including all your models like this:
/**
* Resource type model map.
*
* @see https://docs.opensoutheners.com/laravel-apiable/guide/#getting-started
*/
'resource_type_map' => [
App\Models\Film::class => 'film',
App\Models\Review::class => 'review',
],If you see, this is same as Laravel's Relation::enforceMorphMap() but reversed.
Setup your models
This is a bit of manual work, but you need to setup your models in order for them to be JSON:API serializable entities:
You need to add that implements JsonApiable to your class importing this class and the jsonApiableOptions method.
Basic transformation usage
And, finally, use as simple as importing the class OpenSoutheners\LaravelApiable\Http\Resources\JsonApiCollection for collections or OpenSoutheners\LaravelApiable\Http\Resources\JsonApiResource for resources.
Error handling
When your application returns errors and your frontend only understand JSON:API, then these needs to be transform. So we've you cover, set them up by simply doing the following on your app/Exceptions/Handler.php
Last updated