Using Laravel's Pagination Class with JQuery Masonry and Infinite Scroll

25th July 2012 | Tags:

Just a quick one, but it’s some­thing that I got stuck with for a while.

If you try and use Laravel’s pag­i­na­tion class with JQuery Masonry along with Infi­nite Scroll, you may find a prob­lem. When you scroll down beyond what should be the last page, you’ll prob­a­bly find that you keep get­ting the last page over and and over again, caus­ing gen­uinely infi­nite scrolling. The rea­son for this took me some uncovering.

When Inifi­nite Scroll requests an addi­tional page, it incre­ments the page num­ber, but gets the same con­tent back — how­ever, there is no rea­son why it should know that that’s the case. It’s because it keeps get­ting new con­tent back that it keeps try­ing, but the issue is actu­ally in the Lar­avel Paginator.

In order for Infi­nite Scroll to know when to stop, it expects a page not found; i.e., a 404. How­ever when the Pag­i­na­tion class gets a page num­ber from the GET para­me­ters which is greater than the max­i­mum page num­ber, it sim­ply sets it to the last avail­able page, and returns that con­tent. It’s by no means a bug; just dif­fer­ent behav­iour to that which Infi­nite Scroll expects.

There are a cou­ple of ways around it. You could sim­ply over­ride the Pag­i­na­tor class and change the behav­iour. Or, in your con­troller /​route you could sim­ply inter­cept the results from the pag­i­na­tor and do a return Response::error(404), I guess it depends on the case.


    I was having the same issue. Fixed it by checking the results coming from the paginator and the actual page, like so:

    if (Input::get('page') > $topics->last) {
    return Response::error('404');


    18th January 2013

Links and images are allowed, but please note that rel="nofollow" will be automactically appended to any links.