returning response with json in laravel api removes the collection pagination links

447 Views Asked by At

i'm using laravel for webservice. i want to return a collection like this:

return response()->json([data => $data]);

and i'm using laravel api resouces for transforming the collection. this is my resource:

class Item extends JsonResource
public function toArray($request)
{
    return [
        'id' => $this->id,
        'name'=> $this->name,
        'city_dependent' => $this->city_dependent,
    ];

}

when i pass a collection to the resource like:

$items = Item::where('active' , 1)->paginate(10);
$data = Item::collection($items);

everything works good. if i return the $data, the pagination links and meta are returning and there is no problem.

"links": {
    "first": "http://127.0.0.1:8000/api/category/items?page=1",
    "last": "http://127.0.0.1:8000/api/category/items?page=1",
    "prev": null,
    "next": null
},
"meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 1,
    "path": "http://127.0.0.1:8000/api/category/items",
    "per_page": 10,
    "to": 3,
    "total": 3
}

but if i return the data with response()->json() like below, all data about "meta" and "links" are remove! and everything else is ok.

return response()->json([
        'result' =>true,
        'data' => $data
    ]);

why the pagination data and links are removed from the data? is there anything that i missed?

0

There are 0 best solutions below