# Details

> Describe a resource to clients with a thin base, then ask for fields, relations, actions, metrics and authorizations with the with parameter.

The details endpoint, `GET /{resource}`, describes a resource for the current user: its fields, relations, scopes, actions, metrics, rules, text search, gates and authorizations. A frontend reads it once to build a list, a form or the dialog of an action.

The endpoint answers a thin base. Ask for the rest with the `with` query parameter: what is not asked for is not computed.

## The base

```http
GET /api/artworks
```

```json
{"data": {
  "fields": [
    {"name": "id", "label": null, "type": "id", "nullable": false},
    {"name": "title", "label": "Title", "type": "text", "nullable": false},
    {"name": "medium", "label": null, "type": "enum", "nullable": false, "values": ["oil", "ink", "watercolor"]}
  ],
  "limits": [10, 25, 50],
  "meta": {}
}}
```

- `fields` has one entry per field, in declared order: `name`, `label` (the field's `label()`, or `null`), `type` and `nullable`. An `Enum` field adds `values`, the backing values of the enum, and `offered`, the values that writes accept, when its [`offered()`](/resources/fields#offered-cases) leaves cases out:

```json
{"name": "status", "label": null, "type": "enum", "nullable": false, "values": ["planned", "on_sale", "sold_out"], "offered": ["planned", "on_sale"]}
```

- `limits` lists the page sizes that `search.limit` accepts.
- `meta` holds what the resource's `detailsMeta()` returns, see [below](#meta).
- The endpoint checks `viewAny` on the resource. Without `authorizations`, `actions.authorized` or `metrics.authorized` in `with`, it calls no other policy.

## Asking for more with `with`

`with` is a comma-separated list of sections, such as `actions`, and parts of a section, such as `actions.authorized`:

```http
GET /api/artworks?with=fields.capabilities,actions.authorized
```

A part adds its section with the base keys of that section. A repeated value counts once, and an empty `with` answers the base.

<table>
<thead>
  <tr>
    <th>
      <code>
        with
      </code>
    </th>
    
    <th>
      Adds
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        fields.capabilities
      </code>
    </td>
    
    <td>
      <code>
        select
      </code>
      
      , <code>
        sort
      </code>
      
      , <code>
        group
      </code>
      
      , <code>
        measures
      </code>
      
      , <code>
        create
      </code>
      
       and <code>
        update
      </code>
      
       on each field
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fields.filter
      </code>
    </td>
    
    <td>
      <code>
        filter.operators
      </code>
      
       on each field
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fields.rules
      </code>
    </td>
    
    <td>
      <code>
        rules
      </code>
      
       on each field: <code>
        all
      </code>
      
      , <code>
        create
      </code>
      
       and <code>
        update
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        fields.meta
      </code>
    </td>
    
    <td>
      <code>
        meta
      </code>
      
       on each field
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        relations
      </code>
    </td>
    
    <td>
      each relation: <code>
        resource
      </code>
      
      , <code>
        relation
      </code>
      
       and <code>
        name
      </code>
      
      , plus <code>
        types
      </code>
      
       and <code>
        morph
      </code>
      
       on a <code>
        MorphTo
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        relations.constraints
      </code>
    </td>
    
    <td>
      <code>
        constraints
      </code>
      
       on each relation
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        relations.operations
      </code>
    </td>
    
    <td>
      <code>
        operations
      </code>
      
       and <code>
        sync
      </code>
      
       on each relation
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        relations.pivot_fields
      </code>
    </td>
    
    <td>
      <code>
        pivot_fields
      </code>
      
       on each many-to-many relation
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        actions
      </code>
    </td>
    
    <td>
      each action: <code>
        name
      </code>
      
      , <code>
        uriKey
      </code>
      
      , <code>
        target
      </code>
      
      , <code>
        destructive
      </code>
      
      , <code>
        confirm
      </code>
      
       and <code>
        confirmation
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        actions.fields
      </code>
    </td>
    
    <td>
      the input entries of each action
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        actions.authorized
      </code>
    </td>
    
    <td>
      <code>
        authorized
      </code>
      
       on each action (policy calls)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        actions.meta
      </code>
    </td>
    
    <td>
      <code>
        meta
      </code>
      
       on each action
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        metrics
      </code>
    </td>
    
    <td>
      each metric: <code>
        name
      </code>
      
      , <code>
        uriKey
      </code>
      
      , <code>
        type
      </code>
      
       and <code>
        ranges
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        metrics.authorized
      </code>
    </td>
    
    <td>
      <code>
        authorized
      </code>
      
       on each metric (policy calls)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        metrics.meta
      </code>
    </td>
    
    <td>
      <code>
        meta
      </code>
      
       on each metric
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        instructions
      </code>
    </td>
    
    <td>
      each instruction: <code>
        name
      </code>
      
      , <code>
        uriKey
      </code>
      
       and <code>
        text
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        instructions.fields
      </code>
    </td>
    
    <td>
      the input entries of each instruction
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        instructions.meta
      </code>
    </td>
    
    <td>
      <code>
        meta
      </code>
      
       on each instruction
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        scopes
      </code>
    </td>
    
    <td>
      each scope: <code>
        name
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        scopes.fields
      </code>
    </td>
    
    <td>
      the input entries of each scope
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        scopes.meta
      </code>
    </td>
    
    <td>
      <code>
        meta
      </code>
      
       on each scope
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rules
      </code>
    </td>
    
    <td>
      the rules of the resource
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        text
      </code>
    </td>
    
    <td>
      the text search, <code>
        null
      </code>
      
       without one
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        gates
      </code>
    </td>
    
    <td>
      the names that <code>
        search.gates
      </code>
      
       accepts
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        confirmations
      </code>
    </td>
    
    <td>
      the writes that ask for a <a href="/actions-and-metrics/confirmation#confirming-writes">
        confirmation
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        authorizations
      </code>
    </td>
    
    <td>
      the class-level abilities for the user (policy calls)
    </td>
  </tr>
</tbody>
</table>

Any other value is a `422` on `with` that lists the accepted values:

```json
{
  "message": "The with field accepts these values, separated by commas: fields.capabilities, fields.filter, fields.rules, fields.meta, relations, relations.constraints, relations.operations, relations.pivot_fields, actions, actions.fields, actions.authorized, actions.meta, metrics, metrics.authorized, metrics.meta, instructions, instructions.fields, instructions.meta, scopes, scopes.fields, scopes.meta, rules, text, gates, confirmations, authorizations.",
  "errors": {"with": ["The with field accepts these values, separated by commas: fields.capabilities, fields.filter, fields.rules, fields.meta, relations, relations.constraints, relations.operations, relations.pivot_fields, actions, actions.fields, actions.authorized, actions.meta, metrics, metrics.authorized, metrics.meta, instructions, instructions.fields, instructions.meta, scopes, scopes.fields, scopes.meta, rules, text, gates, confirmations, authorizations."]}
}
```

The parsing is strict:

- `fields` and `limits` are always in the base: `with=fields` is a `422`.
- There is no third level (`actions.fields.rules`) and no wildcard (`*`).
- Values are case sensitive, take no space after a comma, and no empty value between two commas.
- A list, `with[]=gates`, is a `422`: `The with field must be a string.`

### One request per screen

Ask each screen for what it shows:

```http
GET /api/orders?with=fields.capabilities,fields.filter,scopes.fields,actions.authorized
GET /api/orders?with=fields.capabilities,fields.rules,relations.operations,relations.constraints,authorizations
GET /api/orders?with=actions.fields,actions.authorized
```

The first one serves a list (sortable and filterable columns, scopes, bulk action buttons), the second a create or edit form, the third the dialog of an action.

## Fields

```http
GET /api/artworks?with=fields.capabilities,fields.filter,fields.rules,fields.meta
```

```json
{"name": "title", "label": "Title", "type": "text", "nullable": false, "select": true, "sort": true, "group": false, "measures": [], "filter": {"operators": ["=", "!=", "like", "not like", "ilike", "not ilike", "in", "not in"]}, "create": true, "update": true, "rules": {"all": ["string", "max:120"], "create": ["required"], "update": ["filled"]}, "meta": {}}
```

- `select`: accepted in `search.selects` and returned. `sort`: accepted in `search.sorts`.
- `group`: accepted in `search.totals.groups`. `measures`: the measure types that `search.totals.measures` accepts on the field, `[]` when none. Both are `false` and `[]` when the resource refuses totals to the user, see [Totals](/search/totals#in-the-details).
- `filter.operators`: the operators that `search.filters` accept on the field, `[]` when it is not filterable.
- `create` and `update`: writable on that operation.
- `rules.all` holds the type rule, `nullable` and `rules()`. `rules.create` and `rules.update` hold `creationRules()` and `updateRules()`.
- An [`inputOnly()`](/resources/fields#input-only-fields) field is listed as a write-only field: `select` and `sort` are `false`, `filter.operators` is `[]`, and `create` and `update` follow `creatable()`, `updatable()` and `readonly()`:

```json
{"name": "delivery_address", "label": null, "type": "text", "nullable": false, "select": false, "sort": false, "group": false, "measures": [], "filter": {"operators": []}, "create": true, "update": true, "rules": {"all": ["string", "max:120"], "create": [], "update": []}, "meta": {}}
```

- Rules are strings: a pipe string is split, a rule object that casts to a string gives that string, an enum rule gives `enum`, another rule object gives its snake class name, and closures are left out.

See [Fields](/resources/fields).

## Relations

```http
GET /api/artworks?with=relations.constraints,relations.operations
```

```json
{"resource": "App\\Rest\\Resources\\ExhibitionResource", "relation": "exhibitions", "constraints": {"required_on_creation": false, "prohibited_on_creation": false, "required_on_update": false, "prohibited_on_update": false}, "operations": ["create", "update", "attach", "detach", "sync", "toggle"], "sync": "replace", "name": "BelongsToMany"}
```

- `resource` is the class of the related resource. A `MorphTo` with several targets has no `resource` key.
- `operations` lists the operations that the entries of the relation accept in `mutate`, less the ones that it leaves out with [`onlyOperations()` or `withoutOperations()`](/resources/relations#limiting-the-operations), then `sync` on a [`syncable()`](/writing/relations#syncing-child-rows) relation, `delete` last on a `deletable()` relation, `[]` on `HasOneThrough`, `HasManyThrough` and a read-only relation. Read the operations from it, not from `name`. A many-to-many relation without `detach` lists no `toggle`: `"operations": ["attach", "sync"]` for `onlyOperations(MutationOperation::Attach, MutationOperation::Sync)`.
- `sync` tells what a `sync` entry of the relation does:

<table>
<thead>
  <tr>
    <th>
      <code>
        sync
      </code>
    </th>
    
    <th>
      Relation
    </th>
    
    <th>
      A <code>
        sync
      </code>
      
       entry
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        "replace"
      </code>
    </td>
    
    <td>
      a many-to-many relation
    </td>
    
    <td>
      makes the listed rows the attached rows
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        "link"
      </code>
    </td>
    
    <td>
      a many-to-many relation without <code>
        detach
      </code>
      
      , or a custom relation that lists <code>
        sync
      </code>
      
       in <code>
        mutationOperations()
      </code>
    </td>
    
    <td>
      links the listed rows and removes none
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        "check"
      </code>
    </td>
    
    <td>
      a <code>
        HasMany
      </code>
      
       or <code>
        MorphMany
      </code>
      
       with <code>
        syncable()
      </code>
    </td>
    
    <td>
      checks that the list and the entries next to it account for every child, and writes nothing itself, see <a href="/writing/relations#syncing-child-rows">
        Syncing child rows
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        null
      </code>
    </td>
    
    <td>
      a relation that takes no <code>
        sync
      </code>
    </td>
    
    <td>
      
    </td>
  </tr>
</tbody>
</table>

- A `MorphTo` adds `types`, each morph type mapped to its resource class, and `morph`, its `type_column` and `key_column`.
- `relations.pivot_fields` adds `pivot_fields` to each many-to-many relation, with every key of a field entry.

Detect `pivot_fields`, `types` and `morph` by their presence, not by `name`. See [Relations](/resources/relations).

## Actions and metrics

```http
GET /api/artworks?with=actions.fields,actions.authorized,metrics
```

```json
{"data": {
  "actions": [{"name": "Retire Artworks", "uriKey": "retire-artworks", "target": "search",
    "fields": [{"name": "notify", "label": null, "type": "boolean", "nullable": false, "default": false, "rules": ["boolean"], "meta": {}}],
    "destructive": false, "confirm": null, "confirmation": false, "authorized": true}],
  "metrics": [{"name": "Artworks By Medium", "uriKey": "artworks-by-medium", "type": "partition", "ranges": []}],
  "fields": [...],
  "limits": [10, 25, 50],
  "meta": {}
}}
```

- `target` is `search`, `resources` or `none`, see [Actions](/actions-and-metrics/actions#targets).
- `destructive` and `confirm` are hints for clients. `confirmation` is `true` when the action declares a [server-checked confirmation](/actions-and-metrics/confirmation).
- An input entry holds `name`, `label`, `type`, `nullable`, `default`, `rules` and `meta`, plus `values` for an `Enum`, and `offered` when its `offered()` leaves cases out. A frontend can render it as a form.
- `authorized` is `true` when the user passes every ability that the route of the action or the metric checks before it reads the request. A refused one stays in the list with `false`, and its route answers `403`.

## Instructions and scopes

```http
GET /api/artworks?with=instructions,scopes
```

```json
{"instructions": [{"name": "By Medium", "uriKey": "by-medium", "text": true}], "scopes": [{"name": "pricedUnder"}]}
```

An instruction's `text` says whether it can be sent next to a `search.text`. `instructions.fields` and `scopes.fields` add their input entries. See [Scopes and instructions](/search/scopes-and-instructions).

## Rules, text and gates

```http
GET /api/artworks?with=rules,text,gates
```

```json
{
  "rules": {"all": {}, "create": {"price": ["required", "min:0"]}, "update": {}},
  "text": {"fields": ["title"], "modes": ["words", "phrase"], "relevance": false, "trashed": false, "max_length": 255, "engine": null},
  "gates": ["view", "create", "update", "delete"]
}
```

- `rules` holds `rules()`, `createRules()` and `updateRules()` of the resource, keyed by attribute.
- `text` describes the [text search](/search/text-search). On `ScoutEngine`, `engine` describes the grammar of the index, and is `null` on the database:

```json
{"engine": {
  "filters": [{"name": "price", "type": "number", "operators": ["=", "!=", ">", ">=", "<", "<=", "in", "not in"]}],
  "sorts": ["price"],
  "max_sorts": 3, "max_page_size": 250, "window": null,
  "lists": true, "max_list_sorts": 2, "tie_breaker": "ulid", "groups": true,
  "totals": {"groups": ["status"], "measures": {"price": ["sum", "avg", "min", "max"]}, "lists_only": false}
}}
```

- `filters`, `sorts`, `max_sorts`, `max_page_size` and `window` describe what the engine filters, sorts and pages.
- `lists` and `max_list_sorts` show on a resource that serves [engine lists](/search/search-engines#engine-lists) on request, and `tie_breaker` when it declares one.
- `groups` shows, `true`, when the engine accepts [groups of filters](/search/search-engines#groups-of-filters).
- `totals` shows when the engine declares [totals](/search/totals#totals-in-the-details): the grouped attributes, the measure types per attribute, and `lists_only`, `true` when the engine computes totals for engine lists only.
- `gates` lists the abilities that `search.gates` accepts, then the gates that the resource declares, then those that [extensions](/extensions/authorization#declaring-gates) declare. `restore` and `forceDelete` come after `delete` when the model uses `SoftDeletes`. It is `[]` when the gates are off. See [Gates](/authorization/gates).

## Confirmations

```http
GET /api/documents?with=confirmations
```

```json
{"confirmations": {"mutate": false, "destroy": true, "restore": false, "forceDestroy": false}}
```

`confirmations` says, for each write endpoint, whether the resource declares its confirmation hook, such as `destroyConfirmation()`. `restore` and `forceDestroy` appear when the model uses `SoftDeletes`. A `true` endpoint can answer `428`: see [Confirmation](/actions-and-metrics/confirmation#confirming-writes).

## Authorizations

```http
GET /api/artworks?with=authorizations
```

```json
{"authorizations": {"viewAny": {"allowed": true, "message": null}, "create": {"allowed": false, "message": "Only curators add artworks."}}}
```

`authorizations` answers each class-level ability that the routes of the resource check: `viewAny`, `create`, the abilities that `abilities()` returns for each endpoint (`restore` and `forceDestroy` only when the model uses `SoftDeletes`), then those of the actions and the metrics. Row abilities are not here: list them in the [gates](/authorization/gates) of a search. A resource whose `isAuthorizingEnabled()` returns `false` answers `"allowed": true` to every ability.

The answers go through the decisions of the request, and an ability that `authorizationCache()` declares is read from the [cache](/authorization/decision-cache).

## Meta

`detailsMeta()` adds keys under `meta`. It runs on each details request, for its user:

```php
public function detailsMeta(): array
{
    return [
        'label' => 'Artworks',
        'default_sort' => ['field' => 'title', 'direction' => 'desc'],
        'curator' => $this->request()->user()?->name,
    ];
}
```

```json
{"meta": {"label": "Artworks", "default_sort": {"field": "title", "direction": "desc"}, "curator": "Ada"}}
```

An [extension](/extensions/responses-and-checks#adding-details-meta) that implements `AddsDetailsMeta` adds its keys under `meta.{name}`, after the keys of `detailsMeta()`, as an object, `{}` when empty. A `detailsMeta()` key equal to the name of such an extension throws `InvalidMetaExtensionException`.

## Behaviour

- The details list what the declarations of the resource return for the current request, so a list that depends on `$this->request()->user()` changes with the user.
- `actions`, `instructions` and `relations` are lists. Maps are objects, also when empty: `"meta": {}`.
- The `describing()` event runs after `viewAny`, and `described()` receives the details as the request asked for them. See [Events and hooks](/digging-deeper/events-and-hooks).
- A declaration error throws when the request reads that declaration, for example `InvalidTextSearchException` with `with=text`.
- Clients ignore the keys that they do not know: later releases can add keys.
- The OpenAPI document gives each resource a `{Resource}Details` schema, where the base keys are required and the sections and parts optional, and documents the `with` parameter. See [OpenAPI](/digging-deeper/openapi).
