israelnyc's avatar

Pagination not showing on last page with one result

I may be missing something really obvious but I'm trying to use pagination for the first time and it works unless the last page has one result. Then it doesn't show pagination at all (not a style issue; no ul.pagination on the page).

The query:

$posts = Post::orderBy('updated_at', 'desc')->paginate(5);
return view('index',  compact('posts'));

View (also tried with render method):

{{ $posts->links() }}

I have six Post records. The first page displays five records with the pagination links rendered as expected. Clicking on page 2 shows the sixth record but no pagination, so I have to use the browser back button to get back to the first page.

If however I change it to paginate(4), it works fine.

Is this expected behavior?

0 likes
3 replies
SagunKho's avatar
SagunKho
Best Answer
Level 1

I can confirm this. Solution is - in resources/views/vendor/pagination/default.blade.php change at line 1 @if ($paginator->count() > 1) to @if ($paginator->count() >= 1)

EDIT: Default behaviour should be - @if ($paginator->count() >= 1 && ($paginator->hasMorePages() || !$paginator->onFirstPage()))

3 likes
israelnyc's avatar

@SagunKho

Thank you!

Didn't imagine I'd have to vendor:publish and customize for what I would think should be default behavior but once changed, it now works as I expect.

jlrdw's avatar

You should create an issue if there is a problem.

1 like

Please or to participate in this conversation.