Laravel - API Resources - Adding Meta Data
Some JSON API standards require the addition of meta data to your resource and resource collections responses. This often includes things like links
to the resource or related resources, or meta data about the resource itself. If you need to return additional meta data about a resource, include it in your toArray
method. For example, you might include link
information when transforming a resource collection:
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
'links' => [
'self' => 'link-value',
],
];
}
When returning additional meta data from your resources, you never have to worry about accidentally overriding the links
or meta
keys that are automatically added by Laravel when returning paginated responses. Any additional links
you define will be merged with the links provided by the paginator.