I want to have it so in certain situations when I return any class of resource top level meta data is appended by default.
So here's an example of some meta data I'd want to be added globally and how I can currently add it for every response individually.
public function show($id)
{
$resource = new UserResource(User::findOrFail($id));
if (App::environment('local')) {
$queryLog = DB::getQueryLog();
$resource->additional([
'meta' => [
'queries' => [
'count' => count($queryLog),
'log' => $queryLog,
],
],
]);
}
return $resource;
}
Here I'm checking if I'm in a local/development environment and I'm appending the query log to the meta data for this resource.
This will return something akin to
"data": [
...
],
"meta": {
"queries": {
"count": 14,
"log": [
{
"query": "select * from `users` where `users`.`id` in (?) and `users`.`deleted_at` is null",
"bindings": [
1
],
"time": 1.27
},
{
...
However the obvious problem here is I'd need to add the above conditional in every controller action which is cumbersome.
Any way to have this added globally for all resources?