Requests
Includes & sparse fieldsets
How include and fields shape a document, and the settings that bound them.
A client asks for related resources with include and restricts the
fields of each type with fields. Both are read from the
query string only.
GET
/posts/10
?include=author,comments.author
&fields[posts]=title,author
&fields[users]=name
Includes
- Each path names relationships, dot-separated:
comments.authorincludes the comments and their authors. - The included resources are loaded before rendering, one query per relation and per include level.
include.max-depthbounds the deepest path accepted and rendered (null: no limit).- A relationship included renders its
data, whatever its linkage.
Sparse fieldsets
fields[type]lists the attributes and relationships to render for that type; the others are never computed.- By default it restricts the relationships too, as the specification says.
fields.relationshipsset tokeeprenders every relationship whateverfields(the behaviour of 1.x). - A value with
->whenInFields()is rendered only whenfields[type]names it explicitly, never by default. fields[users]=address.citykeeps one member of a struct.
Reading them in your code
Ark4ne\JsonApi\Context\JsonApiContext holds the include tree and the fieldsets of the current request:
$context = app(JsonApiContext::class);
// true when the path is included
$context->include->has('posts.comments');
// true when `title` of `post` is rendered
$context->fields->allows('post', 'title');
In a resource, prefer the request conditions:
whenInFields() and whenInIncludes().
Validating them
Without validation, an unknown path is ignored. Validate both against the definitions with a
JsonApiFormRequest.