How to Customize Simple Page Arrows?

Asked

Viewed 1,008 times

3

It is very good to use the pagination of Laravel, with a command you do everything:

Controller

$artigos->simplePaginate(5);

View

{!! $artigos->render !!}

And after that the pagination is generated.

But by default comes with arrows PREV and NEXT.

How to take them off and put Previous and Next ?

And by default also this pagination is customized with Bootstrap classes. But I’m not using bootstrap and that goes for other developers as well who don’t use.

  • Which version of Laravel?

  • Larave 5.0, 5.1, 5.2. I saw that Laravel 5.3 has a pretty cool scheme as already commented.

  • Yeah, Laravel’s already awesome, and 5.3’s even better...

2 answers

5


There is a simple way that this can be done. Since the Laravel Paginate is based on the Bootstrap theme it is possible to change its values by str_replace() of PHP.

<?php
   $pagination = $artigos->render();
   $pagination = str_replace('&laquo;', 'POSTS RECENTES <i class="fa fa-arrow-right"></i>', $pagination);
   $pagination = str_replace('&raquo;', '<i class="fa fa-arrow-left"></i> MAIS POSTS', $pagination);

   echo $pagination;
?>

That character &laquo; («) is the default left arrow that Laravel set for the prev and the other character &raquo; (») the pattern to the right.

So just make one str_replace(), replacing the arrow with whatever we want, a button, previous, next.

5

According to the documentation of Laravel 5.3 you can customize the view as follows:

Running the command of artisan to transport the view pagination standard for resources/views/vendor/pagination:

php artisan vendor:publish --tag=laravel-pagination

From there, just edit the content HTML of your default.blade.php, which corresponds to default paging.


Also according to the documentation, you can customize the own functionalities of view using the following methods:

$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (Not available when using simplePaginate)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (Not available when using simplePaginate)
$results->url($page)
  • Note that in the case of your question, using simplePaginate(), some methods are not available!

For paging on Laravel 5.2 and earlier

It is enough that instead of using the method render(), do as follows:

@include('paginacao.default', ['paginator' => $artigos]) //onde o primeiro parâmetro é o caminho da sua view

And customize it by creating your Blade, for example:

@if ($paginator->lastPage() > 1)
<ul class="pagination">
    <li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
        <a href="{{ $paginator->url(1) }}">Previous</a>
    </li>
    @for ($i = 1; $i <= $paginator->lastPage(); $i++)
        <li class="{{ ($paginator->currentPage() == $i) ? ' active' : ''     }}">
            <a href="{{ $paginator->url($i) }}">{{ $i }}</a>
        </li>
    @endfor
    <li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
        <a href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a>
    </li>
</ul>
@endif

Where for this version the available methods are:

$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (Not available when using simplePaginate)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (Not available when using simplePaginate)
$results->url($page)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.