Custom pagination in Laravel 4

The default Laravel pagination is built for Twitter Bootstrap 2.0 and I believe that it will be updated to use Twitter Bootstrap 3.0 in Laravel 4.1 (not sure though). So what happens when you want to use the pagination but have to customize it to your needs? It’s actually really simple since Laravel allows you to specify your own pagination view where you can access the pagination object.

If you want a more updated version of this, read my new post: Simple pagination in PHP with the Laravel pagination package.

Specify your own view

Open up app/config/view.php and change the pagination key to point to the view you want. I’ll be using this value:

'pagination' => 'structure/pagination'

This will tell Laravel to not use its default view for pagination but instead use this one when it outputs the pagination.

Customize the view

First you need to create your view, in my case it’s app/views/structure/pagination.blade.php. My own need was to implement it for Semantic UI, so this example shows how you can customize the view entirely with the $paginator object which is an instance of Illuminate\Pagination\Paginator.

@if ($paginator->getLastPage() > 1)
  <?php $previousPage = ($paginator->getCurrentPage() > 1) ? $paginator->getCurrentPage() - 1 : 1; ?>
  <ul class="ui pagination menu">
    <a href="{{ $paginator->getUrl($previousPage) }}"
      class="item{{ ($paginator->getCurrentPage() == 1) ? ' disabled' : '' }}">
      <i class="icon left arrow"></i> Previous
    </a>

    @for ($i = 1; $i <= $paginator->getLastPage(); $i++)
    <a href="{{ $paginator->getUrl($i) }}"
      class="item{{ ($paginator->getCurrentPage() == $i) ? ' active' : '' }}">
      {{ $i }}
    </a>
    @endfor

  <a href="{{ $paginator->getUrl($paginator->getCurrentPage()+1) }}"
    class="item{{ ($paginator->getCurrentPage() == $paginator->getLastPage()) ? ' disabled' : '' }}">
    Next <i class="icon right arrow"></i>
  </a>
</ul>
@endif
  • Luke

    On Line 7, <= should be changed to < , to create $i <= $paginator.

    Thanks for the code, this helped!

    • Luke

      “<“, rather should be changed to <

    • For some reason my blog converts it to a html entity sometimes which is very annoying. Glad you liked the code!

  • Jake Wilson

    You’ve got a bug:

    In the “Previous” link,

    $paginator->getUrl(1)

    should be

    $paginator->getCurrentPage()-1

    Otherwise your Previous link always linked back to the first page no matter what page you are on.

    • Thanks for catching that one, I’ve updated the code now.

  • bitw

    UL change to DIV

  • Septian Si Bhoechie

    Very nice..Thanks.. 😀

  • soleha

    Thanks alot 🙂

  • gj1118

    Thank you sir . much appreciated..

  • gj1118

    A quick question for your sir, do you have an example of working with semantic-ui forms with laravel ? It seems when I use the form element there is an animation that is added to the page as it loads – which is not what I want. Also the semantic-ui suggests I should be using the form class on the div tag to work with forms. In that case how do I specify the form action url and method? please suggest.

    • I don’t work a lot with semantic ui actually, I have only used it in one project and that was a long time ago. So I can’t really help you with that.

  • KimJongChill

    Much appreciated mate!

  • João Batitsa

    Obrigado, from Brasil 🙂

  • mark gxr

    Nice thanks a lot also found a good tut here http://tutsnare.com/how-to-create-pagination-in-laravel/ both make a good sense. Thanks

  • Alex

    Thanks a lot, it’s great =D

  • PerfectM

    Thanks man. I save a lot of time.
    Keep it up. Cheers!