Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

Custom Pagination Page Number Display

I'm trying to develop custom pagination for tables that will be put together using Livewire and there is a specific way the customer wants the pagination displayed. Some of the below code is wrong due to having a problem wrapping my head around so many situations to think of. However, I would happily take suggestions on what it is I need to add, remove, refactor.

Let's say there are 13 pages in this paginator currently on page load and will default to page 1. The small table below demonstrates how I"m attempting to get the correct page numbers to display on the page.

Page Loaded     Page Numbers Visible
1                1, 2, 3, 4, 5, 13
2                1, 2, 3, 4, 5, 13
3                1, 2, 3, 4, 5, 13
4                1, 2, 3, 4, 5, 13
5                1, 4, 5, 6, 13
6                1, 5, 6, 7, 13
7                1, 6, 7, 8, 13
8                1, 7, 8, 9, 13
9                1, 8, 9, 10, 13
10               1, 9, 10, 11, 12, 13
11               1, 9, 10, 11, 12, 13
12               1, 9, 10, 11, 12, 13
13               1, 9, 10, 11, 12, 13
 @if (
    $page > $paginator->lastPage() - 1 ||
    $page == $paginator->onFirstPage() + 1 ||
    $page == $paginator->currentPage() + 1 ||
    $page == $paginator->currentPage() - 1 ||
    $page <= $paginator->onFirstPage() + 5 ||
    $paginator->currentPage() >= $paginator->lastPage() - 5 ||
    ($page < $paginator->onFirstPage() + 5 && $paginator->currentPage() + 1 > $page)
) 
{ 
    // show page number 
}
0 likes
3 replies
vainway 's avatar

men u don't need to do that laravel solved it like this

$posts= Post::orderBy('id', 'desc')->paginate(10); return view('posts.index', ['posts' => $posts]);

jrdavidson's avatar

This has nothing to do with how it is retrieved from the database. This is for custom blade pagination for how it is displayed in the browser.

jlrdw's avatar

You can write any type of custom template you need, just example here:

https://laracasts.com/discuss/channels/guides/paginator-another-episode

Pagination links is just styled.

Also it doesn't make sense sometime having one and sometimes having two, I would go with just two on each side and first and last page.

And note that usually if first or last page is the current page it's good to gray it out, meaning you're on that page.

I guess you could have logic with if page is less than 10.

Please or to participate in this conversation.