amarsyla's avatar

Changing simple pagination next/prev labels

Hello,

I don't have much knowledge on PHP, and I am fresh new to Laravel.

I have successfully implemented the Eloquent pagination, and I am rendering the pagination with this code:

{!! $colleagues->appends(Request::except('page'))->render() !!}

Right now, the previous and next links are represented with '<<' and '>>'. I'd like to change that. I know how to do that by editing the core of Laravel. I managed to do that by inputting my own "Next" and "Previous" strings as parameters to $this->getPreviousButton() and $this->getNextButton() in Illuminate\Pagination\SimpleBootstrapThreePresenter. Is there any way of editing those values with a custom class of mine or something similar?

I tried to add a custom class, and extending the SimpleBootstrapThreePresenter, but no luck. What's the best way to achieve this?

Thanks.

0 likes
6 replies
bestmomo's avatar

The simplest way is to pass your Presenter as parameter of render method. Look at it in core :

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();
}
amarsyla's avatar

Hello @bestmomo, thanks for your reply. I already saw your answer to other questions, but to be honest, I didn't understand very well. Should I create any new file, or edit the Laravel core in this case?

Thanks.

bestmomo's avatar
Level 52

@amarsyla

You create your class presenter maybe by extending SimpleBootstrapThreePresenter :

use Illuminate\Pagination\SimpleBootstrapThreePresenter ;

class myPresenter extends SimpleBootstrapThreePresenter {

  // ...

}

In your controller you create a new myPresenter (just inject it) and you send it to the view.

And you use it as parameter for render :

{!! $colleagues->appends(Request::except('page'))->render($mypresenter) !!}

But should be more elegant to generate links in controller and to send them to the view.

1 like
amarsyla's avatar

Okay, this is my CustomSimplePagination class, stored in app/Library/Pagination:

<?php namespace App\Library\Pagination;

use Illuminate\Pagination\SimpleBootstrapThreePresenter ;

class CustomSimplePagination extends SimpleBootstrapThreePresenter
{
    public function __construct(PaginatorContract $paginator)
    {
        $this->paginator = $paginator;
    }
    
    public function hasPages()
    {
        return $this->paginator->hasPages() && count($this->paginator->items()) > 0;
    }
    
    public function render()
    {
        if ($this->hasPages())
        {
            return sprintf(
                '<ul class="pager">%s %s</ul>',
                $this->getPreviousButton("test1"),
                $this->getNextButton("test2")
            );
        }
    
        return '';
    }
}

On my controller, I added:

use App\Library\Pagination\CustomSimplePagination;

Then, on my method, I added:

$customPaginationPresenter = new CustomSimplePagination;

And, I sent that variable to the view and used it like so:

{!! $colleagues->appends(Request::except('page'))->render($customPaginationPresenter) !!}

All of this should work fine, but when I go to my route, I get this error:

ErrorException in CustomSimplePagination.php line 13:
Argument 1 passed to App\Library\Pagination\CustomSimplePagination::__construct() must be an instance of App\Library\Pagination\PaginatorContract, none given, called in C:\www\htdocs\social\app\Http\Controllers\ProfileController.php on line 33 and defined
1 like
amarsyla's avatar

Okay, I solved this. I just had to add the query variable as a parameter to the Class init like this:

$customPaginationPresenter = new CustomSimplePagination($connections);

Thanks a lot for your help.

Rjs37's avatar

Sorry for bumping up an old thread (seemed the most recent relevant thread) but I just came across this issue myself and I found it rather strange. Just glad that someone else had already come across it.

The simple paginator used to use the lang files to determine the label text like so:

echo $presenter->getPrevious($trans->trans('pagination.previous'));

But now it's manually set, seems strange to have kept the pagination lang file if it's no longer being used by the paginator by default? It seems rather convoluted that I have to extend from the existing Paginator Presenter (and then specify this presenter in all of my render calls) just to restore that pretty useful logic.

Please or to participate in this conversation.