Hello Friends,

Welcome To Infinitbility!

If you want to customize your pagination view and want to understand the flow of Laravel pagination then you are right place. For creating Pagination in Laravel You have to know the basics structure of MVC Pattern. For Custom Pagination added skip 5 pages example

Create Route for pagination view in web.php


use Illuminate\Support\Facades\Route;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/', function () {
    return view('welcome');

Route::get('/users', '[email protected]');

Create UserController.php in Http/Controller folder


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
     * Show the users with paginate.
     * @param  object  $request
     * @return View
    public function users(Request $request)
        $users = User::paginate(10);
        return view('users', compact('users'));

Create Users blade in resource/Views folder

<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    <title>Hello, world!</title>
  <table class="table table-bordered table-dark">
      <th scope="col">#</th>
      <th scope="col">Avatar</th>
      <th scope="col">Email</th>
      <th scope="col">First Name</th>
      <th scope="col">Last Name</th>
      <th scope="col">Created On</th>
  @foreach($users as $user)
      <th scope="row">{{ $user->id }}</th>
      <td> <img src="{{ $user->avatar }}" width="25" height="25" /></td>
      <td>{{ $user->email }}</td>
      <td>{{ $user->first_name }}</td>
      <td>{{ $user->last_name }}</td>
      <td>{{ date('d M, Y', strtotime($user->created_at)) }}</td>

    <!-- For Default pagination user -->
    <div>{{ $users->links() }}</div>

    <!-- For Custom pagination User -->
    <div>{{ $users->links('pagination') }}</div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

Create pagination blade in the views folder

        <ul class="pagination pull-right">
            {{-- Previous Page Link --}}
            @if($paginator->currentPage() > 5)
                <li class="page-item">
                    <a class="page-link" href="<?php echo $paginator->url( $paginator->currentPage() - 5 ); ?>" rel="prev" aria-label="&lsaquo; Skip 5"> &lsaquo; Skip 5 </a>
            @if ($paginator->onFirstPage())
                <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
                    <span class="page-link" aria-hidden="true">&lsaquo;</span>
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>

            {{-- Pagination Elements --}}
            @foreach ($elements as $element)
                {{-- "Three Dots" Separator --}}
                @if (is_string($element))
                    <li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li>

                {{-- Array Of Links --}}
                @if (is_array($element))
                    @foreach ($element as $page => $url)
                        @if ($page == $paginator->currentPage())
                            <li class="page-item active" aria-current="page"><span class="page-link">{{ $page }}</span></li>
                            <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>

            {{-- Next Page Link --}}
            @if ($paginator->hasMorePages())
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">&rsaquo;</a>
                <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
                    <span class="page-link" aria-hidden="true">&rsaquo;</span>
            @if($paginator->lastPage() >= $paginator->currentPage()+5)
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator->url( $paginator->currentPage() + 5 ) }}" rel="prev" aria-label="Skip 5  &rsaquo;">Skip 5 &rsaquo;</a>

Some useful pagination function in Laravel

  1. Get the number of items for the current page.

  2. Get the current page number.

  3. Get the result number of the first item in the results.

  4. Get the paginator options.

  5. Create a range of pagination URLs.

        $results->getUrlRange($start, $end)	    
  6. Determine if there are enough items to split into multiple pages.

  7. Determine if there is more items in the data store.

  8. Get the items for the current page.

  9. Get the result number of the last item in the results.

  10. Get the page number of the last available page. (Not available when using simplePaginate).

  11. Get the URL for the next page.

  12. Determine if the paginator is on the first page.

  13. The number of items to be shown per page.

  14. Get the URL for the previous page.

  15. Determine the total number of matching items in the data store. (Not available when using simplePaginate).

  16. Get the URL for a given page number.

  17. Get the query string variable used to store the page.

  18. Set the query string variable used to store the page.


Laravel Pagination Response

   "total": 50,
   "per_page": 15,
   "current_page": 1,
   "last_page": 4,
   "first_page_url": "http://laravel.app?page=1",
   "last_page_url": "http://laravel.app?page=4",
   "next_page_url": "http://laravel.app?page=2",
   "prev_page_url": null,
   "path": "http://laravel.app",
   "from": 1,
   "to": 15,
            // Result Object
            // Result Object

Thanks for reading…

More tutorial form Laravel

Yajra issue after install in laravel

Laravel call function from another class

laravel pagination with customization

How to solve page expired error in laravel for webhooks, ajax, and form

Laravel Model

Laravel Clear cache, config, view and Routes

how to force Laravel to use https in URL and assets