Support the ongoing development of Laravel.io →
posted 9 years ago
Requests
Last updated 2 years ago.
0

Did you search forum for existing paging issues? There are oodles of existing pagination threads. And l4 or l5?

Last updated 9 years ago.
0

jimgwhit said:

Did you search forum for existing paging issues? There are oodles of existing pagination threads. And l4 or l5?

I did search, and found nothing. Laravel 4 as I marked the thread. Don't know why this isn't displayed.

jimgwhit said:

see
http://laravel.io/forum/01-13-2015-laravel-pagination-return-empty-result-set

I found no solution there. My thread isn't really related to that. I have a problem with pagination when I access non existing pages (page number > number of records / number of records per page). It works for me if the number of records is smaller than the number of records per page. I know what offset is used for, it is used for pagination. And I don't have conflicting offset in my queries. And I don't have multiple paginations on one site.

The following seems related, but has no solution: http://laravel.io/forum/08-24-2014-little-problem-with-pagination

Last updated 9 years ago.
0

Even a workaround like this doesn't work:

$items = Item::whereNull('field')->paginate(20);
if (\Input::get('page') > $items->getLastPage()) { // workaround
    return \Redirect::to($items->getUrl($items->getLastPage()));
}

EDIT: Fixed it! Now at least the workround works.

Last updated 9 years ago.
0

Just fixed this very same problem, using Laravel 5.1. Noticed it when Google had indexed my site with a '/?page=3' added to the url, which didn't cause an error as such but did return an empty result, resulting in a page full of blank.

So, in the controller I'm fetching blog posts in an ordered, paginated Eloquent Collection:

$posts = \App\Post::where('active_yn', 1)->orderBy('created_at', 'desc')->paginate(3);

I've only just started the blog so we're only going to get one result! If I add a ?/page=3 to the URL then we get the empty result bug.

To fix the problem I used the Collection class's isEmpty() method to check for the empty result and redirect the request to the home page, via a named route.

    //detects if an empty dataset is returned 
        if ($posts->isEmpty()) {
            return redirect()->route('index');
        }
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sykano sykano Joined 13 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.