article

Thursday, July 7, 2022

Laravel 9 Pagination Example

Laravel 9 Pagination Example

How to install laravel 9

https://tutorial101.blogspot.com/2022/02/how-to-install-laravel-9.html

Connecting our Database

open .env file root directory.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=root
DB_PASSWORD=

Create tables 

php artisan make:migration create_customers_table
C:\xampp\htdocs\laravel\laravelproject>php artisan make:migration create_customers_table


Open new migrations
yourproject/database/migrations
laravelproject\database\migrations\create_customers_table.php
//laravelproject\database\migrations\create_customers_table.php
return new class extends Migration
{
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('CustomerName');
            $table->string('Address');
            $table->string('City');
            $table->string('PostalCode');
            $table->string('Country');
            $table->timestamps();
        });
    }
};
Database Migration
php artisan migrate

C:\xampp\htdocs\laravel\laravelproject>php artisan migrate
Migration table created successfully.
check database table

Create tables Model

php artisan make:model Customers

Creating Controller

php artisan make:controller CustomersController
C:\xampp\htdocs\laravel\laravelproject>php artisan make:controller CustomersController
change it with the following codes:
laravelproject\app\Http\Controllers\CustomersController.php
//laravelproject\app\Http\Controllers\CustomersController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class CustomersController extends Controller
{
    function index()
    {
        $data = DB::table('customers')->orderBy('id', 'desc')->paginate(3);
        return view('index', compact('data'));
    }
}
Route
laravelproject\routes\web.php
//laravelproject\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomersController; //add CustomersController

//Route::get('/', function () {
//    return view('welcome');
//});
  
Route::get('/', [CustomersController::class, 'index']);
Create Views
laravelproject\resources\views\index.blade.php

Bootstrap 5 https://getbootstrap.com/docs/5.0/getting-started/introduction/
https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css
//laravelproject\resources\views\index.blade.php
 <!DOCTYPE html>
 <html>
 <head>
 <title>Laravel 9 Pagination Example </title>
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" />
 </head>
 <body>
 <div class="container">
     <h3 align="center">Laravel 9 Pagination Example </h3> <br />
     <div class="row">
     <div class="col-12">
         <div class="table-responsive">
             <table class="table table-striped table-bordered">
             <thead>
                 <tr>
                 <th>ID </th>    
                 <th>Customer Name </th>
                 <th>Address </th>
                 <th>City </th>
                 <th>Postal Code </th>
                 <th>Country </th>
                 </tr>
             </thead>
             <tbody>
            @if(!empty($data) && $data->count())
                @foreach($data as $rs)
                 <tr>
                     <td>{{ $rs->id }} </td>
                     <td>{{ $rs->CustomerName }} </td>
                     <td>{{ $rs->Address }} </td>
                     <td>{{ $rs->City }} </td>
                     <td>{{ $rs->PostalCode }} </td>
                     <td>{{ $rs->Country }} </td>
                 </tr>
                @endforeach
            @else
                 <tr>
                     <td colspan="6">There are no data. </td>
                 </tr>
            @endif    
             </tbody>
             </table>
         </div>
     </div>    
     </div>
     <div class="row">{{ $data->links() }} </div>
 </div>
 </body>
 </html>
Register bootstrap
//laravelproject\app\Providers\AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        //
    }

    public function boot()
    {
        Paginator::useBootstrap(); //https://laravel.com/docs/8.x/pagination#using-bootstrap
    }
}
Run C:\xampp\htdocs\laravel\laravelproject>php artisan serve
Starting Laravel development server: http://127.0.0.1:8000

Related Post