create pagination in laravel

The main of this blog is to create simple 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 Routes
  5. 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.



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->string('name');  //add this line
            $table->text('address'); //add this line

    public function down()

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.



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()



namespace Database\Seeders;

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

class DataSeeder extends Seeder
    public function run()

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 :



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'));

Create View

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 "


Adding Routes

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



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

Testing Our Application

php artisan serve

open below URL into your browser :

In output it will display name and address of 10 people and provides link of next and previous. When user click on that link it work accordingly.

Share your thoughts

Ask anything about this examples