Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom pagination view in Laravel 5

Laravel 4.2 has the option to specify a custom view in app/config/view.php such as:

/* |-------------------------------------------------------------------------- | Pagination View |-------------------------------------------------------------------------- | | This view will be used to render the pagination link output, and can | be easily customized here to show any view you like. A clean view | compatible with Twitter's Bootstrap is given to you by default. | */ 'pagination' => 'pagination_slider-alt' 

This is gone in Laravel 5 at least regarding view.php.

Is there a way to replicate this behavior in Laravel 5?

like image 799
user2094178 Avatar asked Jan 30 '15 16:01

user2094178


People also ask

How can I use pagination in Laravel?

Use Pagination in LaravelRemove all() and use paginate() and it takes a number as an argument, that number defines the number of results to be shown to the user. To display the pagination component on the view, we need to add the following code in the home. blade. php file below the table component.


1 Answers

In Laravel 5.3+ use

$users->links('view.name') 

In Laravel 5.0 - 5.2 instead of

$users->render() 

use

@include('pagination.default', ['paginator' => $users]) 

views/pagination/default.blade.php

@if ($paginator->lastPage() > 1) <ul class="pagination">     <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">         <a href="{{ $paginator->url(1) }}">Previous</a>     </li>     @for ($i = 1; $i <= $paginator->lastPage(); $i++)         <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">             <a href="{{ $paginator->url($i) }}">{{ $i }}</a>         </li>     @endfor     <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">         <a href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a>     </li> </ul> @endif 

That's it.


If you have a lot of pages, use this template:

views/pagination/limit_links.blade.php

<?php // config $link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now) ?>  @if ($paginator->lastPage() > 1)     <ul class="pagination">         <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">             <a href="{{ $paginator->url(1) }}">First</a>          </li>         @for ($i = 1; $i <= $paginator->lastPage(); $i++)             <?php             $half_total_links = floor($link_limit / 2);             $from = $paginator->currentPage() - $half_total_links;             $to = $paginator->currentPage() + $half_total_links;             if ($paginator->currentPage() < $half_total_links) {                $to += $half_total_links - $paginator->currentPage();             }             if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) {                 $from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1;             }             ?>             @if ($from < $i && $i < $to)                 <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">                     <a href="{{ $paginator->url($i) }}">{{ $i }}</a>                 </li>             @endif         @endfor         <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">             <a href="{{ $paginator->url($paginator->lastPage()) }}">Last</a>         </li>     </ul> @endif 
like image 166
Mantas D Avatar answered Oct 16 '22 14:10

Mantas D