Getting started

Introduction

Render Eloquent models, arrays and objects as JSON:API 1.1 documents, described once and validated.

Laravel JSON:API renders your models as JSON:API 1.1 documents. A resource describes its members with typed descriptors ($this->string(), $this->one(UserResource::class)); the package compiles that description once per class into a definition, validates include and fields against it, loads the relations a document needs in batch, and renders only what the request asks for.

app/Http/Resources/PostResource.php
#[JsonApiType('posts')]
final class PostResource extends JsonApiResource
{
    public function toAttributes(Request $request): iterable
    {
        return [
            'title' => $this->string(),
            'published-at' => $this->datetime('published_at'),
        ];
    }

    protected function toRelationships(Request $request): iterable
    {
        return [
            'author' => $this->one(UserResource::class),
            'comments' => $this->many(CommentResource::class),
        ];
    }
}

In this documentation

  • Resources: the hooks of a resource, its values, conditions, relationships, links and meta.
  • Requests: includes and sparse fieldsets, their validation, write requests and error documents.
  • API revisions: how a resource keeps serving the clients pinned on an older revision.
  • Tooling: jsonapi:check, static analysis and deployment.
  • Integrations: Laravel's native JSON:API resources and OpenAPI.

Compatibility

VersionPHP Laravel Status
3.x^8.3^12.46 || ^13.0Active
2.x^8.3^12.46 || ^13.0End of life
1.x^8.1^10.0 || ^11.0 || ^12.0 || ^13.0End of life

Next

Install the package, then follow the quick start.