Laravel 5 custom pagination view example
In this post we will give you information about Laravel 5 custom pagination view example. Hear we will give you detail about Laravel 5 custom pagination view exampleAnd how to use it also give you demo for it if it is necessary.
laravel default also provide pagination view using bootstrap design like next and previous button and number page with link. but it is a very comman and generally used by most of developer. If you want to change it using Presenter class of laravel. you can set your own custom view for your pagination layout.
In bellow i give you the example of i added first, last, next and previous button and main banifit is that you can easily modify that view and link. so let’s see the preview of image layout of pagination.
Preview:
So, first create new folder “Pagination”app/Pagination and inside this folder create one file “HDPresenter.php”app/Pagination/HDPresenter.php. this class file will generate custom layout for pagination. in this class file you can add your own class, div, id etc. it is very easy to understand how it works. so put bellow code in that file.
app/Pagination/HDPresenter.php
namespace AppPagination;
use IlluminatePaginationBootstrapThreePresenter;
class HDPresenter extends BootstrapThreePresenter {
public function render()
{
if ($this->hasPages()) {
return sprintf(
'<div ><div >%s %s</div> <div >%s %s</div></div>',
$this->getFirst(),
$this->getButtonPre(),
$this->getButtonNext(),
$this->getLast()
);
}
return "";
}
public function getLast()
{
$url = $this->paginator->url($this->paginator->lastPage());
$btnStatus = '';
if($this->paginator->lastPage() == $this->paginator->currentPage()){
$btnStatus = 'disabled';
}
return $btn = "<a href='".$url."'><button class='btn btn-success ".$btnStatus."'>Last <i class='glyphicon glyphicon-chevron-right'></i></button></a>";
}
public function getFirst()
{
$url = $this->paginator->url(1);
$btnStatus = '';
if(1 == $this->paginator->currentPage()){
$btnStatus = 'disabled';
}
return $btn = "<a href='".$url."'><button class='btn btn-success ".$btnStatus."'><i class='glyphicon glyphicon-chevron-left'></i> First</button></a>";
}
public function getButtonPre()
{
$url = $this->paginator->previousPageUrl();
$btnStatus = '';
if(empty($url)){
$btnStatus = 'disabled';
}
return $btn = "<a href='".$url."'><button class='btn btn-success ".$btnStatus."'><i class='glyphicon glyphicon-chevron-left pagi-margin'></i><i class='glyphicon glyphicon-chevron-left'></i> Previous </button></a>";
}
public function getButtonNext()
{
$url = $this->paginator->nextPageUrl();
$btnStatus = '';
if(empty($url)){
$btnStatus = 'disabled';
}
return $btn = "<a href='".$url."'><button class='btn btn-success ".$btnStatus."'>Next <i class='glyphicon glyphicon-chevron-right pagi-margin'></i><i class='glyphicon glyphicon-chevron-right'></i></button></a>";
}
}
Now, how to use this class in laravel blade file, so see following file how to extend this class on pagination class function.
In Blade file
@extends('layouts.app')
@section('content')
<div >
<table >
<tr>
<th>Title</th>
<th>Description</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ $post->title }}</td>
<td>{{ $post->description }}</td>
</tr>
@endforeach
</table>
{!! with(new AppPaginationHDPresenter($posts))->render(); !!}
</div>
@endsection
Hope this code and post will helped you for implement Laravel 5 custom pagination view example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs