Links & meta
Links of a resource
protected function toLinks(Request $request): ?iterable
{
return [
'self' => route('api.user.show', ['id' => $this->resource->id]),
];
}
toLinks() returns plain values: it holds no descriptor.
Links by route
#[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.selfof the resource object comes from theselfroute, which must exist (jsonapi:checkfails otherwise).links.selfandlinks.relatedof each relationship come from the routesposts.relationships.{relationship}andposts.{relationship}, derived from the prefix ofself, when they exist. Name them withrelationship:andrelated:, orfalsefor none.- The links the resource declares (
toLinks(),->links()) win, evennull.
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.
Links of a relationship
'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.