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]);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
}
Please or to participate in this conversation.