How to can we make pagination in Laravel

39 views
Skip to first unread message

Nawang Sherab Jamtsho

unread,
Oct 16, 2013, 4:28:39 AM10/16/13
to lar...@googlegroups.com
Can anyone help me making pagination for a Blog post in Laravel faramework 4

Sid Young

unread,
Sep 6, 2015, 8:46:49 PM9/6/15
to Laravel, njw...@gmail.com
This might work as basic pagination logic?

TOTAL_ROWS = select count(*) from table;

PAGES = TOTAL_ROWS / PAGE_SIZE       # (set PAGE_SIZE to the number of rows to show in a screen "page"

if( (PAGES x PAGE_SIZE) < TOTAL_ROWS) PAGES++;      # capture the last few rows that dont fill the last page completely 

Page=1;       Start at first page #
LastID=0;     First row is 1 so >0 logic starts at 1 in SQL

#repeat this part

ROWS = select * from table where id > LastID orderby ID LIMIT PAGE_SIZE
foreach(ROWS as ROW)
   LastID=ROW->id;

So you now have a page full of data in ROWS, the last row ID used for the next SQL fetch.

For the Page selection you could store the row ID of the first row in an array so if you need to fetch back a random page you can decrement by 1 and re-fetch from there.

Laravel should have a Pagination facility that does this or most of this, look into "chunck" command...

Sid
Reply all
Reply to author
Forward
0 new messages