I build a Laravel API using resources
. It return a collection
plus some extra meta data. I want to remove the null
item(s) without losing my formatted JSON
response.
Note: I tried filter
method but it removed "data" and "extra_meta" from the JSON
response. In other word it altered the collection
heavily and it didn't just removed null
item(s).
{
"data": [
{
"data_1": "some data"
},
{
"data_1": "some data"
},
null,
null,
null,
null,
null,
null
],
"extra_meta": {
"meta_1": "some meta"
}
}
Here is a simplified code using resource
return $collection = someResource::collection($somthing)->additional([
'extra_meta' => [
'meta_1' => $request->metaInfo,
],
]);