Quantcast
Channel: ronggur – MySandbox
Viewing all articles
Browse latest Browse all 13

Laravel 5.1 Pretty Pagination URL

$
0
0

I’m currently working on Laravel 5.1 websites and need some modification to laravel pagination. So in this post we will create laravel 5.1 pretty pagination URL. This steps will produce http://yourdomain.com/section/page/2 pretty pagination url

Add Paginator to your Controller.php

//
use Illuminate\Pagination\Paginator;
//

Include your paginator view template to your main.blade.php view

/*
* $objectData : objects from your model
*/

@include('pagination', [ 'paginator' => $objectData, 'args'=>[] ])
//

Next is to create pagination.blade.php view to be included in your main view

<?php
/* get current page */
$page = $paginator->currentPage();

/* get last page */
$lastPage = $paginator->lastPage();

if($page<=1){
    $prevPage = URL::route( app('request')->route()->getAction()['as'], array_merge($args, [ 'page' => 1 ]) );
}else{
    $prevPage = URL::route( app('request')->route()->getAction()['as'], array_merge($args, [ 'page' => $page-1 ]) );
}

if($page <= $lastPage){
    $nextPage = URL::route( app('request')->route()->getAction()['as'], array_merge($args, [ 'page' => $page+1 ]) );
}else{
    $nextPage = URL::route( app('request')->route()->getAction()['as'], array_merge($args, [ 'page' => $lastPage ]) );
}
?>

@if ($paginator->lastPage() > 1)
<ul class="pagination">

    /* show if only current page greater than 1 */
    @if($page>1)
        <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
            <a href="{{ $prevPage }}" class="prevnext ready" icon="r"></a>
        </li>
    @endif
    
    /* start looping the pagination */
    @for ($i = 1; $i <= $paginator->lastPage(); $i++)
        <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
            <?php
            $url = URL::route( app('request')->route()->getAction()['as'], array_merge($args, [ 'page' => $i ]) );
            ?>
            <a href="{{ $url }}">{{ $i }}</a>
        </li>
    @endfor
    
    /* show if only we're not at the last page */
    @if($page<$lastPage)
        <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
            <a href="{{ $nextPage }}" class="prevnext ready" icon="s"></a>
        </li>
    @endif
</ul>
@endif

The last but crucial, don’t forget to create your route

/* 
* don't forget to add `?` to your route 
*/

Route::get('/{page?}', [ 'as' => 'section', 'uses' => 'Controller@index' ] );
//

 

The post Laravel 5.1 Pretty Pagination URL appeared first on MySandbox.


Viewing all articles
Browse latest Browse all 13

Trending Articles