I want to format pagination in laravel like this:
api/posts?from=1&to=10
I tried:
$posts = new LengthAwarePaginator(Post::all(), 100, 10);
return $posts->toArray();
Which didn’t work at all :( Please help
You can just return the pagination object as shown in the documentation.
your url should be api/posts?limit=10
and you can use in your controller as
Post::paginate(request('limit'));
it generates
{
"total": 50,
"per_page": 15,
"current_page": 1,
"last_page": 4,
"first_page_url": "http://laravel.app?page=1",
"last_page_url": "http://laravel.app?page=4",
"next_page_url": "http://laravel.app?page=2",
"prev_page_url": null,
"path": "http://laravel.app",
"from": 1,
"to": 15,
"data":[
{
// Result Object
},
{
// Result Object
}
]
}
to get the next page in pagination your url should be as api/posts?page={page number}&limit=10
limit can be changed according to requirement