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.
Table of contents
- Installation
- Usage
- Relationships
- Resource depth
- Exception handler
- Example
- Deprecated
registermethod - Tweaking responses
- License
Installation
Require the package
Usage
- Let your resource object extend from
JsonApiResourceinstead ofJsonResource. - Set the type of your resource as a string in
$this->type. - For each part of your resource, define the matching
to{resourcePart}method.
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.
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
Example usage
Defining resources via the register method
In the previous version of the package, you would have to define the resource structure via a register method. This is still possible, but it is deprecated and will be removed in a later version.
To use this way of defining a resource, simply define a register method in your resource:
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.
License
JsonApi is open-sourced software licensed under the MIT Licence