mastermindlegion's avatar

Pagination data[] changes to data{}

Hello guys, I got a problem regarding pagination. When i'm at the first ?page=1 data is [ ] and at the ?page=2 data is {}. Thank you very much I don't know how to proceed. Here is my controller:

public function index(Request $request, Tag $tag)
    {
     $collection = ArticleResource::collection($tag->articles);
     return $collection->paginate(1);
    }

?page=1 response is also quite weird is that there is no meta object { } being created...

{
"current_page": 1,
"data": [
{
"id": 1,
"user": {
"id": 1,
"full_name": "Martin Chalupa",
"email": "[email protected]"
},
"slug": "ethan-howell-v-1",
"name": "Ethan Howell V",
"body": "Non quo sit ipsum placeat dignissimos. Et repellendus sequi consequatur aut veritatis officia hic. Dolores voluptate et et et velit sit consequatur.",
"tags": [
{
"id": 1,
"slug": "php",
"name": "PHP"
},
{
"id": 2,
"slug": "laravel",
"name": "Laravel"
}
],
"time": {
"created_at": "2020-05-27T09:35:49.000000Z",
"updated_at": "2020-05-27T09:35:49.000000Z"
}
}
],
"first_page_url": "articles?page=1",
"from": 1,
"last_page": 2,
"last_page_url": "articles?page=2",
"next_page_url": "articles?page=2",
"path": "articles",
"per_page": 1,
"prev_page_url": null,
"to": 1,
"total": 2
}

?page=2 response is:

{
"current_page": 2,
"data": {
"1": {
"id": 2,
"user": {
"id": 1,
"full_name": "Martin Chalupa",
"email": "[email protected]"
},
"slug": "kayleigh-kshlerin-1",
"name": "Kayleigh Kshlerin",
"body": "Aperiam ipsum quos atque dolore. Rem ea quos dolor ad commodi recusandae nihil. Rem omnis adipisci velit quos. Eos maiores eos et omnis aut sit accusantium. Quo quod nostrum veritatis ut ex.",
"tags": [
{
"id": 3,
"slug": "ruby",
"name": "Ruby"
},
{
"id": 4,
"slug": "javascript",
"name": "Javascript"
},
{
"id": 1,
"slug": "php",
"name": "PHP"
},
{
"id": 2,
"slug": "laravel",
"name": "Laravel"
}
],
"time": {
"created_at": "2020-05-27T09:35:49.000000Z",
"updated_at": "2020-05-27T09:35:49.000000Z"
}
}
},
"first_page_url": "articles?page=1",
"from": 2,
"last_page": 2,
"last_page_url": "articles?page=2",
"next_page_url": null,
"path": "articles",
"per_page": 1,
"prev_page_url": "articles?page=1",
"to": 2,
"total": 2
}
0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

Probably better to paginate the query, no? The APIResource should handle the LengthAwarePaginator consistently

return ArticleResource::collection($tag->articles()->paginate());

Did you really intend for only one Article per page???

mastermindlegion's avatar

@tykus hello there, for now yes; I got only three articles there so it is for testing. Gonna change it later on

tykus's avatar

You are (i) selecting only the records for the current page, so the query and hydrated models for the request are limited only to those needed; (ii) you are passing a LengthAwarePaginator instance to the collection method on the API Resource, so it properly builds the pagination meta data in the response.

Please or to participate in this conversation.