Integrations

Laravel's native JSON:API

How this package compares with, and works alongside, Laravel's own JSON:API resources.

Laravel (12.46+, 13) ships Illuminate\Http\Resources\JsonApi\JsonApiResource, a lean implementation of the specification that suits many APIs. This package stays independent of it and interoperates with it.

What each one covers

Laravel's nativeThis package
id as a string, "data": null, primary data out of includedyesyes
Attributes resolved after the sparse fieldsetyesyes
Relationships that are not includedleft outlinkage loaded in batch, or left out (relationship.linkage)
Typed values: nullable, date format, decimal precision, enumnoyes
Definition of a resource, validation of include and fieldsnoyes
Links and meta per relationship, whenIncluded(), can(), filtersnoyes
Resources that are not Eloquent modelsnoyes
Public contract for the tooling (OpenAPI)noyes
Error documents, content negotiationnoyes
API revisionsnoyes

Relating native resources

A relationship may relate native resources:

protected function toRelationships(Request $request): iterable
{
    return [
        'author' => $this->one(NativeUserResource::class),
        'tags' => $this->many(NativeTagResource::class),
    ];
}
  • Their linkage is rendered and loaded in batch as any other; links(), meta() and identifierMeta() receive the native resource. A native resource is guessed like any other (#[UseResource], then the naming convention).
  • Included, they are rendered as leaves by their own resolve(), with their fields[type]: their relationships are never rendered, nothing is loaded nor included under them, and the Includes rule rejects include=author.posts.
  • Their fields are unknown without a model: the Fields rule accepts those of their type as they are.
  • A polymorphic relationship (morph()) cannot render native resources.

A native JsonApiRequest is read like any request.