Bootstrap 2 pagination in Laravel 5

Warning - Old content ahead!

This page has been marked as a legacy post. This means it's quite old (from 2015) and probably out of date. Take it with a grain of salt!

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.

<?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.

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

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

<?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;');
  }
}

Spread the word

Share this article

Like this content?

Check out some of the apps that I've built!

Snipline

Command-line snippet manager for power users

View

Pinshard

Third party Pinboard.in app for iOS.

View

Rsyncinator

GUI for the rsync command

View

Comments