Download the PHP package ark4ne/laravel-json-api without Composer
On this page you can find all versions of the php package ark4ne/laravel-json-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ark4ne/laravel-json-api
More information about ark4ne/laravel-json-api
Files in ark4ne/laravel-json-api
Package laravel-json-api
Short Description A Lightweight JSON:API Resource for Laravel
License MIT
Informations about the package laravel-json-api
JsonApi - Laravel Resource
A Lightweight {JSON:API} Resource for Laravel.
Installation
Config
Path | Type | Description |
---|---|---|
describer.nullable |
bool |
For describer notation, defined if a value is nullable by default. |
describer.date |
string datetime format |
For describer notation, defined default date time format. |
describer.precision |
int \ null |
For describer notation, decimal precision for float value. null for disable rounding. |
describer.when-has |
bool \ string[] |
For describer notation, Apply automatically whenHas condition on attributes. |
relationship.when-included |
bool |
Allow to disabled by default the loading of relationship data. |
Usage
This package is a specialisation of Laravel's JsonResource
class.
All the underlying API's are still there, thus in your controller you can still interact
with JsonApiResource
classes as you would with the base JsonResource
class
Request
This package allows the reading and dynamic inclusion of resources that will be requested in the requests via the "include" parameter.
@see {json:api} fetching-includes
Resource attributes will also be filtered according to the "fields" parameter.
@see {json:api} fetching-fields
You can also very simply validate your requests for a given resource via the rules Rules\Includes
and Rules\Fields
.
Include validation
Rules\Includes
will validate the include
to exactly match the UserResource schema (determined by the relationships).
Fields validation
Rules\Fields
will validate the fields
to exactly match the UserResource schema (determined by the attributes and relationships).
Customize validation message
Trans key | default |
---|---|
validation.custom.jsonapi.fields.invalid |
The selected :attribute is invalid. |
validation.custom.jsonapi.fields.invalid_fields |
":resource" doesn \' t have fields ":fields". |
validation.custom.jsonapi.fields.invalid_resource |
":resource" doesn \' t exists. |
validation.custom.jsonapi.includes.invalid |
The selected :attribute is invalid. |
validation.custom.jsonapi.includes.invalid_includes |
":include" doesn \' t have relationship ":relation". |
Resource
Implementable methods :
Example:
toType
Returns resource type.
Default returns model class in kebab case : App\Models\MyPost
=> my-post
toIdentifier
@see {json:api} resource-identifier
Returns resource identifier.
Default returns model id.
toAttributes
@see {json:api} resource-attributes
Returns resource attributes.
Laravel conditional attributes
@see laravel: eloquent-conditional-attributes
Support laravel conditional attributes.
Described attributes
@see described notation
toRelationships
@see {json:api} resources-relationships
Returns resource relationships.
All relationships must be created with ModelResource::relationship
.
This allows the generation of the schema representing the resource and thus the validation of request includes.
If your relation should have been a collection created via the ::collection(...)
method, you can simply use ->asCollection()
.
If you want the relation data to be loaded only when it is present in the request include, you can use the ->whenIncluded()
method.
toRelationships
must returns an array, keyed by string, of JsonApiResource
or JsonApiCollection
.
Laravel conditional relationships
@see laravel: eloquent-conditional-relationships
Support laravel conditional relationships.
Described attributes
@see described notation
Relation links and meta
@see {json:api}: relation-linkage
@see {json:api}: relation-meta
Returns links and meta for a relation.
toLinks
@see {json:api}: resource-linkage
Returns resource links.
toResourceMeta
@see {json:api}: resource-meta
@see {json:api}: document-meta
Returns resource meta.
toMeta
@see {json:api}: document-meta
Returns document meta.
Collection
@see laravel: resource-collection
Collection are implemented in JsonApiCollection
.
Usage is the same as laravel collections.
Described notation
Value methods
Method | Description |
---|---|
bool |
Cast to boolean |
integer |
Cast to integer |
float |
Cast to float |
string |
Cast to string |
date |
Cast to date, allow to use custom format |
array |
Cast to array |
mixed |
Don't cast, return as is |
enum |
Get enum value. |
Relation methods
Method | Description |
---|---|
one |
For relationship with a single value: HasOne , BelongsTo , ... |
many |
For relationship with many value: HasMany , BelongsToMany , ... |
Enum
Method enum
allow to get enum value for backed enum or name for unit enum.
According to structure:
The following attributes resource:
Will return:
All versions of laravel-json-api with dependencies
illuminate/collections Version ^9.0|^10.0|^11.0
illuminate/http Version ^9.0|^10.0|^11.0
illuminate/support Version ^9.0|^10.0|^11.0