Laravel Pagination

Eloquent Example

$page_size = 1;
$products = ProductM::where('cat_id', $cat->id)
    ->orderBy('sort_order', 'asc')
    ->paginate($page_size);

HTML

@php
    $class = '';
    if($products->onFirstPage() == true){
        $class = 'disabled';
    }
@endphp
<li class="page-item {{ $class }}">
    <a class="page-link" href="{{ $products->previousPageUrl() }}"><i class="bi bi-arrow-left-short"></i></a>
</li>

@for($i = 1; $i <= $products->lastPage(); $i++)
    @php 
    $active = '';
    if($i == $products->currentPage()){
        $active = 'active';
    }

    $url = $products->url($i);
    @endphp
    <li class="page-item {{ $active }}"><a class="page-link" href="{{ $url }}">{{ $i }}</a></li>
@endfor

@php
    $class = '';
    if($products->hasMorePages() == false){
        $class = 'disabled';
    }
@endphp
<li class="page-item {{ $class }}">
    <a class="page-link" href="{{ $products->nextPageUrl() }}"><i class="bi bi-arrow-right-short"></i></a>
</li>

Common Functions

lastPage();//Number of last page
count(); //Item count of this page
total(); //Item count of all items
currentPage();
lastPage();
previousPageUrl()
nextPageUrl()
onFirstPage()
hasMorePages()
hasPages()
perPage()
items()//Page size
$paginator->url($page)

Customized URL

$products->withPath('/product/search?keyword=' . $keyword);
$products->appends(['keyword' => $keyword]);
$users = User::paginate(15)->withQueryString();

Reference: