Download the PHP package brainstud/json-api-resource without Composer
On this page you can find all versions of the php package brainstud/json-api-resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brainstud/json-api-resource
More information about brainstud/json-api-resource
Files in brainstud/json-api-resource
Package json-api-resource
Short Description Build JSON:API compliant Laravel API resources
License MIT
Informations about the package json-api-resource
JSON:API Resource for Laravel
Make your Laravel API JSON:API compliant with the Brainstud\JsonApi
package.
Example usage
Installation
Require the package
Usage
- Let your resource object extend from
JsonApiResource
instead ofJsonResource
. - Implement a
register
method that returns the following array. The register has access to$this->resource
which contains the current model
Relationships
JSON:API: Includes
For the relationships to be included they need to be loaded. This can be done by implementing a ?include
parameter or using spatie/laravel-query-builder.
Tweak response
The register
method doesn't have access to $request
like toArray
of JsonResource
has.
If you want to manipulate the response based on the request this can be done by overriding the addToResponse
method.
`
Resource depth
The resource depth has a default of 2. This can be changed by passing an array to the resource where the second item is the required resource depth. In the following example we use a depth of 3:
Which allows us to ask for an include nested 3 levels deep: /courses/{identifier}?include=content,content.answers,content.answers.feedback
Exception handler
This package contains an exception handler to render exceptions as JSON:API error messages.
Either use this handler directly by editing your app.php
and registering this singleton
Or register your own exception handler and delegate the render to the JsonApiExceptionHandler::render
method.
Return error response
There are multiple ways to return an error page
License
JsonApi is open-sourced software licensed under the MIT Licence