mattl
mattl

Reputation: 2242

Laravel 5 Pagination Customisation

In Laravel 5, I am using simplePagination as outlined in the docs. I would like to customise the output so instead of double chevrons &rdaquo; '>>', I could put a right arrow. However I can't seen anywhere to customise it.

Does anyone know where the documentation for this is? Or where to begin looking?

Upvotes: 1

Views: 1644

Answers (1)

kajetons
kajetons

Reputation: 4580

While it is undocumented, it is certainly possible. It's pretty much the same as for Laravel 4. Basically all you need to is create a custom presenter and wrap the paginator instance.

Here's how a presenter might look like:

use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Contracts\Pagination\Presenter;
use Illuminate\Pagination\BootstrapThreeNextPreviousButtonRendererTrait;
use Illuminate\Pagination\UrlWindow;
use Illuminate\Pagination\UrlWindowPresenterTrait;

class CustomPresenter implements Presenter
{
    use BootstrapThreeNextPreviousButtonRendererTrait, UrlWindowPresenterTrait;

    private $paginator;

    private $window;

    public function __construct(Paginator $paginator, UrlWindow $window = null)
    {
        $this->paginator = $paginator;
        $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get();
    }

    public function render()
    {
        if ($this->hasPages()) {
            return sprintf(
                '<ul class="pagination">%s %s %s</ul>',
                $this->getPreviousButton("Previous"),
                $this->getLinks(),
                $this->getNextButton("Next")
            );
        }

        return null;
    }

    public function hasPages()
    {
        return $this->paginator->hasPages() && count($this->paginator->items() !== 0);
    }

    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>'.$text.'</span></li>';
    }

    protected function getActivePageWrapper($text)
    {
        return '<li class="active"><span>'.$text.'</span></li>';
    }

    protected function getDots()
    {
        return $this->getDisabledTextWrapper("...");
    }

    protected function currentPage()
    {
        return $this->paginator->currentPage();
    }

    protected function lastPage()
    {
        return $this->paginator->lastPage();
    }

    protected function getAvailablePageWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';

        return '<li><a href="'.htmlentities($url).'"'.$rel.'>'.$page.'</a></li>';
    }
}

Then from your controller:

    public function index()
    {
        $users = User::paginate(5);
        $presenter = new CustomPresenter($users);

        return view("home.index")->with(compact('users', 'presenter'));
    }

The view:

@foreach ($users as $user)
    <div>{{ $user->email }}</div>
@endforeach
{!! $presenter->render() !!}

Upvotes: 1

Related Questions