Simple pagination example in Laravel 8

In our last Laravel article, we have discussed how to create dummy data using tinker. In this article, we will show list of products using pagination.

Pagination is important feature that provides users to navigate and view articles page by page. Laravel provides pagination feature out-of the box. Laravel's paginate() and simplePaginate() method sends data into pagination and show pagination into blade view.

We will start tutorial from the last article, In that article, we have already created dummy data that we will show into pagination. So if you don't have enough data to show into pagination, you can find way to create dummy data.

Step 1: Create ProductController class

In the first step after project setup, we need to create a controller which will send view into response. So create a controller using below command into Terminal.

php artisan make:controller ProductController

This will create a ProductController file at app/Http/Controllers folder. Now open the file and add index() method which will return view.


namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
     * show product list view.
     * @return \Illuminate\Http\Response
    public function index()
        $products = Product::simplePaginate(10);

        return view('products', compact('products'));

Step 2: Create a route

In this step, we will create a route which will handle controller method. Open routes/web.php file and add a new route into it.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

Route::get('products', [ProductController::class, 'index'])->name('products');

Step 3: Create blade view

In this step, we will create a blade view which will show product list. So create a file resources/views/products.blade.php and add below code into it.

<!doctype html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>All products</title>
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <div class="container m-5">
        <h1>All products</h1>
        <div class="row">
            <div class="col-12">
                <table class="table table-hover">
                            <th scope="col">#</th>
                            <th scope="col">Name</th>
                            <th scope="col">Description</th>
                        @if(count($products) > 0)
                            @foreach ($products as $product)
                                    <th scope="row">{{ $product->id }}</th>
                                    <td>{{ $product->name }}</td>
                                    <td>{{ $product->description }}</td>
                                <th colspan="3" class="text-center">No data found</th>
=                            </tr>
                {!! $products->links() !!}

If you want to customize pagination view, first you need to vendor:publish with following command.

php artisan vendor:publish

And then enter the number of Tag: laravel-pagination, For example 16 in my case:

 Which provider or tag's files would you like to publish?:
  [0 ] Publish files from all providers and tags listed below
  [1 ] Provider: Facade\Ignition\IgnitionServiceProvider
  [2 ] Provider: Fruitcake\Cors\CorsServiceProvider
  [3 ] Provider: Illuminate\Foundation\Providers\FoundationServiceProvider
  [4 ] Provider: Illuminate\Mail\MailServiceProvider
  [5 ] Provider: Illuminate\Notifications\NotificationServiceProvider
  [6 ] Provider: Illuminate\Pagination\PaginationServiceProvider
  [7 ] Provider: Laravel\Sail\SailServiceProvider
  [8 ] Provider: Laravel\Sanctum\SanctumServiceProvider
  [9 ] Provider: Laravel\Tinker\TinkerServiceProvider
  [10] Tag: cors
  [11] Tag: flare-config
  [12] Tag: ignition-config
  [13] Tag: laravel-errors
  [14] Tag: laravel-mail
  [15] Tag: laravel-notifications
  [16] Tag: laravel-pagination
  [17] Tag: sail
  [18] Tag: sail-bin
  [19] Tag: sail-docker
  [20] Tag: sanctum-config
  [21] Tag: sanctum-migrations 

This will import blade views of pagination at resources/views/vendor/pagination folder. Laravel has basic two pagination methods. paginate() method will show few page number also where as simplePaginate() method will only show Previous and Next button.

I hope the article will help you.