Custom Pagination In Laravel

The main of this blog is to create simple custom pagination which will work with database.

For your better understanding we have divided program into some parts.

  1. Create Model And Migrations
  2. Create Controller
  3. Create Blade File
  4. Adding Custom Pagination
  5. Adding Routes
  6. Test Our Application

let's assume you have Laravel application. If you don't have any application then create new application try further steps into it.

Create Model and Migrations

In this example, we will display pagination data from database so we need to create database table. In Laravel, we need to create migration for creating database table and model to access those data into effective ways.

To create model and migration you need to enter below command into terminal :

    
php artisan make:model Data -mfs
    

Here, we have pass additional parameter mfs m is for migrations, F is for factory and S for seeder. Those files will used for generating dummy data to database.

Let's make changes in all files one by one.

database\migrations\2021_11_01_032504_create_data_table.php

    
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDataTable extends Migration
{
    public function up()
    {
        Schema::create('data', function (Blueprint $table) {
            $table->id();
            $table->string('name');  //add this line
            $table->text('address'); //add this line
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('data');
    }
}
    

Now, we have to migrate our database. Enter below command into terminal :

    
php artisan migrate
    

This command will generate Data table into our database. let's add some dummy data into our database using factory and seeder.

database\factories\DataFactory.php

    
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class DataFactory extends Factory
{
    public function definition()
    {
        return [
            'name' => $this->faker->name(),
            'address' => $this->faker->address()
        ];
    }
}
    

database\seeder\DataSeeder.php

    
<?php

namespace Database\Seeders;

use App\Models\Data;
use Illuminate\Database\Seeder;

class DataSeeder extends Seeder
{
    public function run()
    {
        Data::factory()->count(100)->create();
    }
}
    

This alteration will apply logic for creating 100 dummy data while you run below command.

    
php artisan db:seed --class=DataSeeder
    

Create Controller

Let's create controller which will handle operation for pagination and return view with data.

    
php artisan make:controller DataController
    

This command will create DataController.php file at \App\Http\Controller. This newly controller has only one method which will get all data from our model and return it to view. Let's make modification for that :

app\Http\Controller\DataController.php

    
<?php

namespace App\Http\Controllers;

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

class DataContoller extends Controller
{
    public function index()
    {
        $data = Data::paginate(10);
        return view('data', compact('data'));
    }
}
    

Adding Routes

In this step, we just need to define route. For that make following changes to route file. Here, we have created /view-data route which will call index method of DataController.

routes\web.php

    
<?php

use App\Http\Controllers\DataController; //import
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});
Route::get('/view-data', [DataController::class,'index']); //add this line
    

Adding Custom Pagination

For Custom pagination either we can create new view file which will handle pagination or modify existing views.

Using Laravel Paginator view files

We can use default files for modification but in some case you want to use both pagination then you need to create new file which will handle our custom pagination request. For that create file anywhere under views directory with .blade.php extension.

resources\views\custom_pagination.blade.php

In this step, we will create view which will display data that passed through controller to user in table format with pagination links. For data display we will use bootstrap to styling data and table to display formatted data. Let's make changes to view "

resource\views\data.blade.php


Testing Our Application

    
php artisan serve
    

open below URL into your browser :

    
http://127.0.0.1:8000/view-data
    

In output it will display name and address of 10 people. Here it display custom custom links for pagination which we applied into our view.


Share your thoughts

Ask anything about this examples