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


<?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', 'UserController@users');

Create UserController.php in Http/Controller folder



<?php

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">
  <head>
    <!-- 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>
  </head>
  <body>
  <table class="table table-bordered table-dark">
  <thead>
    <tr>
      <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>
    </tr>
  </thead>
  <tbody>
  @foreach($users as $user)
    <tr>
      <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>
    </tr>
  @endforeach
  </tbody>
</table>

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

    <!-- For Custom pagination User -->
    <div>{{ $users->links('pagination') }}</div>
</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>
  </body>
</html>

Create pagination blade in the views folder


@if($paginator->hasPages())
    <nav>
        <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>
                </li>
            @endif
            @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>
            @else
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>
                </li>
            @endif

            {{-- 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>
                @endif

                {{-- 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>
                        @else
                            <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
                        @endif
                    @endforeach
                @endif
            @endforeach

            {{-- 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>
            @else
                <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
                    <span class="page-link" aria-hidden="true">&rsaquo;</span>
                </li>
            @endif
            @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>
                </li>
            @endif
        </ul>
    </nav>
@endif

Some useful pagination function in Laravel

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

        $results->count()
    
  2. Get the current page number.

        $results->currentPage()	                
    
  3. Get the result number of the first item in the results.

        $results->firstItem()	                  
    
  4. Get the paginator options.

        $results->getOptions()  
    
  5. Create a range of pagination URLs.

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

        $results->hasPages()	                  
    
  7. Determine if there is more items in the data store.

        $results->hasMorePages()	              
    
  8. Get the items for the current page.

        $results->items()	                      
    
  9. Get the result number of the last item in the results.

        $results->lastItem()	                  
    
  10. Get the page number of the last available page. (Not available when using simplePaginate).

        $results->lastPage()	
    
  11. Get the URL for the next page.

        $results->nextPageUrl()	             
    
  12. Determine if the paginator is on the first page.

        $results->onFirstPage()	      
    
  13. The number of items to be shown per page.

        $results->perPage()	                   
    
  14. Get the URL for the previous page.

        $results->previousPageUrl()	           
    
  15. Determine the total number of matching items in the data store. (Not available when using simplePaginate).

        $results->total()	                      
    
  16. Get the URL for a given page number.

        $results->url($page)	                 
    
  17. Get the query string variable used to store the page.

        $results->getPageName()	               
    
  18. Set the query string variable used to store the page.

        $results->setPageName($name)	         
    

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,
   "data":[
        {
            // 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