Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using limit parameter in paginate function

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?

like image 232
Damjan Krstevski Avatar asked Apr 18 '17 13:04

Damjan Krstevski


People also ask

What is offset and limit in pagination?

The limit option allows you to limit the number of rows returned from a query, while offset allows you to omit a specified number of rows before the beginning of the result set. Using both limit and offset skips both rows as well as limit the rows returned.

How can I get pagination page numbers in laravel?

In your case you can use $articles->links() in your view to generate the pagination navigation buttons. But if you want to manually set the page then you can do this. $articles = Articles::paginate(5, ['*'], 'page', $pageNumber); The default paginate method takes the following parameters.

How does laravel pagination work?

Pagination works by selecting a specific chunk of results from the database to display to the user. The total number of results is calculated and then split between pages depending on how many results you want to return on a page.


1 Answers

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.

like image 165
patricus Avatar answered Sep 20 '22 02:09

patricus