robindboer's avatar

Pagination with a custom starting point

Hey Everyone,

Im building a RESTful webservice and when a user requests a collection I want them to be able to specify a limit and a starting point as URL-params.

Currently i'm calling paginate on my Eloquent model and giving the limit as an argument. However I cant find a way to let users specify a starting point as URL-param.

I hope everything is clear, and someone has a solution to my problem.

Thanks,

Robin

0 likes
4 replies
RachidLaasri's avatar

Hi,

I am not really sure about what u mean by "a starting point".

What you should do is make your API returns something like

{
"total":500,
"per_page":100,
"current_page":1,
"last_page":5,
"from":1,
"to":100,
"data":[
    {Your collection here}
    ]
}

so, that the user can request any page they want, by adding the current_page queryString to the URL.

robindboer's avatar

Thanks for your suggestion,

However let me clarify what I mean by starting point. I want my users to be able to specify a specific item within a collection as starting point of the response.

So when a user posts a GET request to 'myawesomeapp.dev/api/v1/games?limit=2&start=5' they get a paginated result starting at item nr.5 in the collection.

RachidLaasri's avatar
Level 41

You can do that in your query, same as doing

select *from users limit 5,100

I believe you can do that with skip methode like

DB::table('users')->skip(5)->take(50)->get();

Where 5 is the start and 50 is the limit.

1 like
robindboer's avatar

Thanks, I hoped there was an easier way i.e. to tell Laravel's paginator to start at a certain item.

1 like

Please or to participate in this conversation.