Resources

Links & meta

Links of resources and relationships, from closures or from your routes, and meta of resources and documents.
protected function toLinks(Request $request): ?iterable
{
    return [
        'self' => route('api.user.show', ['id' => $this->resource->id]),
    ];
}

toLinks() returns plain values: it holds no descriptor.

#[JsonApiRoutes] builds the links from the routes of the application, each route receiving the resource id:

use Ark4ne\JsonApi\Resources\Attributes\JsonApiRoutes;

#[JsonApiRoutes(self: 'posts.show')]
final class PostResource extends JsonApiResource {}
  • links.self of the resource object comes from the self route, which must exist (jsonapi:check fails otherwise).
  • links.self and links.related of each relationship come from the routes posts.relationships.{relationship} and posts.{relationship}, derived from the prefix of self, when they exist. Name them with relationship: and related:, or false for none.
  • The links the resource declares (toLinks(), ->links()) win, even null.

The resource id is the only parameter given to the routes: give the others with URL::defaults(). A relationship route without {relationship} in its name (relationship: 'posts.relationships') also receives the relationship name as its relationship parameter.

'author' => $this->one(UserResource::class)->links(
    self: fn() => route('posts.relationships.author', $this->resource),
    related: fn() => route('posts.author', $this->resource),
),

self and related declared by name win over the links of a closure given as first argument, and a link resolving to null is left out. The definition knows these names, so tools documenting the API can tell which links a relationship may render.

Meta of a resource

protected function toResourceMeta(Request $request): ?iterable
{
    return [
        'created_at' => $this->datetime(),
        'updated_at' => $this->datetime(),
    ];
}

Meta of the document

protected function toDocumentMeta(Request $request): ?iterable
{
    return [
        'copyright' => fn() => 'Copyright 2026 My API',
    ];
}

Only the primary resources add document meta, never the included ones. In a collection, the first resource setting a key keeps it, and the collection's own toDocumentMeta() wins over those of its resources (see Collections).

The jsonapi member

Every document carries the top-level jsonapi member, {"version": "1.1"} by default. document.jsonapi sets its version, ext, profile and meta, or false leaves it out.