Bootstrap 2 pagination in Laravel 5

Posted on Mar 21, 2015

Laravel Laravel 5 Bootstrap 2 Twitter Bootstrap

I recently had to help port a website that used Twitter Bootstrap 2 to Laravel 5 and discovered that changing the pagination template is completely different in Laravel 5.

Thanks to this StackOverflow thread I came up with this solution.

In short: Laravel 5 makes use of presenter classes to style pagination.

You can put this class wherever you like. It's down to how you structure your project.

In this example I put the class in a file named BootstrapTwoPresenter.php which lives inside of a newly created app/Presenters directory.

The class inherits from Laravel 5's BootstrapThreePresenter because Bootstrap 2 and Bootstrap 3 pagination are very similar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php namespace App\Presenters;
 
use Illuminate\Pagination\BootstrapThreePresenter;
 
class BootstrapTwoPresenter extends BootstrapThreePresenter
{
  public function render()
  {
    if( ! $this->hasPages())
      return '';

    return sprintf(
      '<div class="pagination"><ul>%s %s %s</ul></div>',
      $this->getPreviousButton(),
      $this->getLinks(),
      $this->getNextButton()
    );
  }
}

In your views you can insert the new class into the render method for the desired results.

1
{!! $users->render(new App\Presenters\BootstrapTwoPresenter($users)) !!}

If you wish to customise the pagination further then the following methods are available to override.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php namespace App\Presenters;
 
use Illuminate\Pagination\BootstrapThreePresenter;
 
class BootstrapTwoPresenter extends BootstrapThreePresenter
{
  public function render()
  {
    if( ! $this->hasPages())
      return '';

    return sprintf(
      '<div class="pagination"><ul>%s %s %s</ul></div>',
      $this->getPreviousButton(),
      $this->getLinks(),
      $this->getNextButton()
    );
  }

  protected function getDisabledTextWrapper($text)
  {
    return '<li class="disabled"><a href="#">'.$text.'</a></li>';
  }
  protected function getActivePageWrapper($text)
  {
    return '<li class="active"><a href="#">'.$text.'</a></li>';
  }
  protected function getDots()
  {
    return $this->getDisabledTextWrapper('&hellip;');
  }
}

Like this article? Sign up to updates - no spam!