Laravel 8 Pagination Example with Bootstrap Tutorial

In this tutorial you will learn about the Laravel 8 Pagination Example with Bootstrap Tutorial and its application with practical example.

In this Laravel 8 Pagination Example with Bootstrap Tutorial, I will show you how to create and use pagination using bootstrap in laravel application. In this tutorial you will learn to implement and use laravel in-built bootstrap pagination to display data. In this article I will share example how to use laravel inbuilt pagination with bootstrap table. This tutorial will demonstrate you how to use laravel pagination with tables and display data with pagination.

Laravel 8 Pagination Example with Bootstrap Tutorial

In this step by step tutorial I will guide you through how to implement bootstrap pagination in laravel application. Please follow the instruction given below:

  1. Install Laravel App
  2. Database Configuration
  3. Model and Migrations
  4. Generate Fake Data
  5. Create Controller & Route
  6. Use Pagination in Laravel
  7. Laravel Custom Pagination Parameter
  8. Convert Pagination Results To JSON
  9. Run Laravel Application

Install Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

switch to the project directory.

Database Configuration

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Model and Migrations

Now, in this step we will create model and migration file. Please run the following command:

Open database/migrations/timestamp_create_employees_table.php file and add the schema.

Now put the following code in the app/Models/Employee.php file to register the schema in the $fillable array.

Now, run the migration to create database table using following artisan command:

Generate Fake Data using Faker

Open the database/seeds/DatabaseSeeder.php file and place the following code.

Execute the given command to generate the data.

Create Controller & Route

Now, lets create a controller named EmployeeController using command given below –

Place the following code in app/Http/EmployeeController.php file.

app/Http/EmployeeController.php

Create Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Render Records in View

In this step we will create a blade view file resources/views/home.blade.php and place the following code inside of it to render the employee records in Laravel view using Bootstrap Table component.

Use Pagination in Laravel

Now, we will add the laravel paginate function in the getData() function inside the EmployeeController class. We will use paginate() instead all() and pass number as an argument, that number defines the number of records to be shown to the user.

Now, we need to add the following code in the home.blade.php file below the table component.

Here is the final home.blade.php file.

home.blade.php

Laravel Custom Pagination Parameter

In this step we will add pagination links/url along with results. It will actually appends ?page=2.

Here, you also need to import and define useBootstrap() inside the boot function within the app/Providers/AppServiceProvider.php:

Convert Pagination Results To JSON

The Laravel paginator result classes implement the Illuminate\Contracts\Support\Jsonable Interface contract and expose the toJson method. This will transform your pagination results into JSON. Open routes/web.php file and place the following route with paginate function.

You can get the pagination JSON data on URL:


Run Laravel Application

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 8 Pagination Example with Bootstrap Tutorial and its application with practical example. I hope you will like this tutorial.