Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BlueScreenJunky's avatar

Is there a simple way to customize the paginatorPresenter in Laravel 5 ?

Hi everyone,

I've started playing with laravel 5 and I'm a little confused by the paginator presenter. I want to display a simplePaginator at the bottom of my page (which is incredibly easy if you want the default bootstrap one) but I want to add a text to the buttons to have something like "< Previous" and "next >" instead of just "<" and ">".

I manage to get what I want but it seems a little convoluted so I think I may have missed something.

What I had to do is create a new PaginatorPresenter class which extends SimpleBootstrapThreePresenter, and overrides the render() method by a copy/paste of the original one except I pass "< Previous" and "Next >" as arguments to $this->getPreviousButton() and $this->getNextButton().

<?php
namespace App;

use Illuminate\Pagination\SimpleBootstrapThreePresenter;

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

        return '';
    }
}

then I can get my HTML by doing :

$articles->render(new \App\PaginationPresenter($articles))

Is there a simpler way to do this ? Like maybe setting a default paginator for all the application ? Or sending just the strings you want to the default paginator ?

Thanks.

0 likes
3 replies
bestmomo's avatar

When looking at the render function :

/**
 * Render the paginator using the given presenter.
 *
 * @param  \Illuminate\Contracts\Pagination\Presenter|null  $presenter
 * @return string
 */
public function render(Presenter $presenter = null)
{
    if (is_null($presenter) && static::$presenterResolver)
    {
        $presenter = call_user_func(static::$presenterResolver, $this);
    }

    $presenter = $presenter ?: new SimpleBootstrapThreePresenter($this);

    return $presenter->render();
}

Presenter can be setting with a $presenterResolver.

SimpleBootstrapThreePresenter is only the default if no resolver.

mtpultz's avatar

Hi @bestmomo,

I created a FoundationFivePresenter by extending the BootstrapThreePresenter and just changing the getDisabledTextWrapper and getActivePageWrapper and placed it in /app/Pagination. I found the function you mentioned above, but I didn't want to instantiate my presenter and pass it in everytime I use render in the view. So I looked at LengthAwarePaginator, since in my controller I'm calling User::paginate(15) and not using the simplePaginator, and the only other way to set $presenterResolver seems to be by extending LengthAwarePaginator and setting it to default to a new instance FoundationFivePresenter in render, somehow instantiating LengthAwarePaginator and calling presenter and passing in a new instance of my presenter which seems even more cumbersome. Is there an easier way to use a custom paginator in Laravel 5.1 that I'm missing? I checked the config files for a setting just in case.

Thanks

bestmomo's avatar

@mtpultz

I think you can set it in a service provider :

use Illuminate\Pagination;
use somenamespace\FoundationFivePresenter;

...

public function register()
{
    Paginator::presenter(function($paginator)
    {
            return new FoundationFivePresenter($paginator);
    });
}
2 likes

Please or to participate in this conversation.