LaraBABA's avatar

Laravel 5.5 API pagination

Hello,

I would like to know if there is a simply way to generate a Json Pagination when building an API using Laravel 5.5.

I was trying to create a pagination using a framework called Framework 7 but it is so difficult(and I am running out of time),that I was hoping for another solution, having the pagination done via Json instead so I can simply pull these 3 below urls in my buttons without much logic:

  "links": {
    "self": "http://example.com/articles",
    "next": "http://example.com/articles?page[offset]=2",
    "last": "http://example.com/articles?page[offset]=10"
  },

After researching a little bit on the internet I found this: https://github.com/spatie/laravel-json-api-paginate

But this confused me, I am using the query builder(not Eloquent), and I thought that this: https://laravel.com/docs/5.4/pagination#paginating-query-builder-results

would produce the correct pagination.

Could anyone please tell me if I really need the Spatie plugin or if there is a way to generate the json api and paginate it with the original plugin in Laravel 5.5 with perhaps something like:

return response()
            ->json($obj)
            ->paginate(15);

Thank you,

0 likes
5 replies
LaraBABA's avatar

Thanks, I think I will go with the eloquent idea, it gives what I was looking for in Laravel 5.5

LaraBABA's avatar
LaraBABA
OP
Best Answer
Level 10

Ok I got it working by following the post and video here: https://kollox.com/news.php?read=laravel-5-api-pagination---how-to-paginate-laravel-5-api

You need to make use of the Laravel resource command to create filtered api outputs, it is a bit tricky at first but then really makes sense after all. I can now remove or allow data in my api outputs but also add details like "show or not show" certain data set base on the user role(example).

Please or to participate in this conversation.