Description
I want to add global top-level meta data.
https://laraveljsonapi.io/docs/3.0/responses/#meta-response
This document doesn't tell you enough in terms of how you can add top-level meta data.
I achieved this by using the index() method.
MyController extends BaseController extends JsonApiController
// BaseController.php
use LaravelJsonApi\Contracts\Routing\Route;
use LaravelJsonApi\Contracts\Store\Store;
public function index(Route $route, Store $store)
{
$response = parent::index($route, $store);
if ($response instanceof DataResponse) {
// Add any top-level metadata
$response = $response->withMeta([
'foo' => 'bar',
'server_time' => now()->toIso8601String(),
]);
// Add any global custom headers
$response = $response->withHeader('X-API-Version', '1.0');
}
return $response;
}
In the documentation its stated as:
Meta Response
Our LaravelJsonApi\Core\Responses\MetaResponse class allows you to return a a JSON:API response that has a top-level meta member, but no data member. This is allowed by the specification.
To use this class, you just need to provide the meta value to either the constructor or the static make method. The meta value can be an array or a Laravel collection.
For example:
use LaravelJsonApi\Core\Responses\MetaResponse;
return new MetaResponse(['foo' => 'bar']);
// or
return MetaResponse::make(['foo' => 'bar'])
->withHeader('X-Foo', 'Bar');
What is the best approach to achieving or leveraging this MetaResponse()
?
Thanks.