Using limit parameter in paginate function

Damjan Krstevski picture Damjan Krstevski · Apr 18, 2017 · Viewed 11.8k times · Source

Is it possible to use 'limit' parameter in paginate() function?

I'm trying this:

$users->where(...)->limit(50)->paginate($page)

...and now, if I have 100 users in the database then the response from paginate function will be all 100 users instead of 50 (or number of users defined with limit parameter).

So, my question is: is it possible to implement limit parameter when I use paginate function?

Answer

patricus picture patricus · Apr 18, 2017

No, it's not possible to limit the query when using pagination.

Query pagination uses skip() and limit() internally to select the proper records. Any limit() applied to the query will be overwritten by the pagination requirements.

If you'd like to paginate a subset of results, you will need to get the results first, and then manually create a paginator for those results.