Getting started

Quick start

From a model to a compound document with includes and sparse fieldsets.

This page builds a blog: posts, their author and their comments.

Generate the resources

Terminal
php artisan make:jsonapi-resource PostResource --model=Post
php artisan make:jsonapi-resource UserResource --model=User
php artisan make:jsonapi-resource CommentResource --model=Comment

Each command writes a resource from the casts, fillable attributes and relations of the model. Edit them to describe what the API exposes:

/**
 * @extends JsonApiResource<Post>
 */
#[JsonApiType('posts')]
final class PostResource extends JsonApiResource
{
    public function toAttributes(Request $request): iterable
    {
        return [
            'title' => $this->string(),
            'excerpt' => $this->string(
                fn(Post $post) => mb_substr((string) $post->body, 0, 40),
            ),
            'published-at' => $this->datetime('published_at'),
            'rating' => $this->float()->precision(1)->whenNotNull(),
        ];
    }

    protected function toRelationships(Request $request): iterable
    {
        return [
            'author' => $this->one(UserResource::class),
            'comments' => $this->many(CommentResource::class)
                ->links(related: fn() => "https://api.example.com/posts/"
                    . "{$this->resource->id}/comments")
                ->meta(fn() => ['total' => $this->resource->comments->count()]),
        ];
    }

    protected function toLinks(Request $request): ?iterable
    {
        return [
            'self' => "https://api.example.com/posts/{$this->resource->id}",
        ];
    }
}

Return them from a controller

app/Http/Controllers/PostController.php
public function show(Post $post): PostResource
{
    return PostResource::make($post);
}

public function index(): JsonApiCollection
{
    return PostResource::collection(Post::query()->paginate());
}

Do not load the relations yourself: the package loads what the document renders, before rendering, one query per relation and per include level.

Request a compound document

GET
/posts/10
?include=author,comments.author
&fields[users]=name
Response200 OK
{
  "jsonapi": { "version": "1.1" },
  "data": {
    "type": "posts",
    "id": "10",
    "attributes": {
      "title": "JSON:API",
      "excerpt": "A specification for building APIs in JSO",
      "published-at": "2026-01-02T03:04:05+00:00",
      "rating": 4.3
    },
    "relationships": {
      "author": { "data": { "type": "users", "id": "1" } },
      "comments": {
        "data": [{ "type": "comments", "id": "100" }],
        "links": { "related": "https://api.example.com/posts/10/comments" },
        "meta": { "total": 1 }
      }
    },
    "links": { "self": "https://api.example.com/posts/10" }
  },
  "included": [
    { "type": "users", "id": "1", "attributes": { "name": "Ada" } },
    {
      "type": "comments",
      "id": "100",
      "attributes": { "content": "First!" },
      "relationships": { "author": { "data": { "type": "users", "id": "2" } } }
    },
    { "type": "users", "id": "2", "attributes": { "name": "Alan" } }
  ]
}
  • include=author,comments.author puts the author, the comments and their authors in included.
  • fields[users]=name: the users carry their name only.
  • id is always a string, rating is rounded to one decimal, published-at is formatted.
  • email is left out: whenInFields() renders it only when fields[users] names it.
  • posts of a user renders no data: whenIncluded() keeps it for documents that include it.

Validate the query

An unknown include should be a 422, not a silent no-op. A form request validates include and fields against the definitions:

app/Http/Requests/PostShowRequest.php
use Ark4ne\JsonApi\Http\JsonApiFormRequest;

final class PostShowRequest extends JsonApiFormRequest
{
    protected string $resource = PostResource::class;
}

?include=author.unknown now answers 422 with a JSON:API error pointing at the include parameter.

Check the resources

Terminal
php artisan jsonapi:check --strict

Run it in CI: it fails on a resource that cannot be described (see Checking resources).

Next