API Resources

API Resources transform a model into JSON in a dedicated class: you choose the exposed fields, rename them or format the values. This separates the database structure from the response and avoids leaking internal fields. Resource::collection handles a list.

Example

public function toArray($request): array
{
  return ['id' => $this->id, 'nom' => $this->nom];
}

Related concepts

The whole glossary