How To Create Simple Customizing Pagination In Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework.

How to Create Simple Customizing Pagination with laravel

1.Create Model and Controller using below command.

php artisan make:model demo -mcr


2.Create route in web.php

Route::get('custom-pagination','DemoController@index');


3.Create demo.blade.php in resource\veiw folder as below code
 
<!DOCTYPE html>
<html>
<head>
    <title>Customizing pagination in Laravel </title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    <br>
    <br>
    <div class="text-center">
        <h1>Customizing pagination in Laravel</h1>
    </div>
    <br>
    <br>

    <div class="well ">
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                <tr class="heading">
                    <th>Name</th>
                    <th>Description</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($demo as $product)
                    <tr>
                        <td>{!! $product->name !!}</td>
                        <td>{!! $product->desc !!}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {!! $demo->links('pagination') !!}
        </div>

    </div>
    <br>
</div>
</body>
</html>


4.Create pagination.blade.php in resource\veiw folder as below code
@if ($paginator->hasPages())
    <ul class="pager">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="disabled"><span>← Previous</span></li>
        @else
            <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">← Previous</a></li>
        @endif


        {{-- Pagination Elements --}}
        @foreach ($elements as $element)
            {{-- "Three Dots" Separator --}}
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
            @endif


            {{-- Array Of Links --}}
            @if (is_array($element))
                @foreach ($element as $page => $url)
                    @if ($page == $paginator->currentPage())
                        <li class="active my-active"><span>{{ $page }}</span></li>
                    @else
                        <li><a href="{{ $url }}">{{ $page }}</a></li>
                    @endif
                @endforeach
            @endif
        @endforeach


        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">Next →</a></li>
        @else
            <li class="disabled"><span>Next →</span></li>
        @endif
    </ul>
@endif


5.Implement code as below code in democontroller
<?php

namespace App\Http\Controllers;

use App\Models\demo;
use App\Todo;
use Illuminate\Http\Request;

class TodoController extends Controller
{
    public function index(Request $request){
        $demo=demo::paginate(5);
        return view('demo',compact('demo'));
    }
    
}


6.create demo model as below 
<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class demo
 * @package App\Models
 * @version September 30, 2019, 6:44 am UTC
 *
 * @property string name
 * @property string desc
 */
class demo extends Model
{
    use SoftDeletes;

    public $table = 'demos';
    

    protected $dates = ['deleted_at'];


    public $fillable = [
        'name',
        'desc'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'name' => 'string',
        'desc' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        
    ];
}

 

Related Post