Question :
It is very good to use Laravel’s paging, with a command if it does everything:
Controller
$artigos->simplePaginate(5);
View
{!! $artigos->render !!}
And after that the pagination is generated.
But by default comes the PREV and NEXT arrows.
How to remove them and put Previous and Next ?
And by default this pagination is also customized with Bootstrap classes. But I’m not using bootstrap and this goes for other developers too who do not use.
Answer :
There’s a simple way this can be done. Since Laravel’s 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('«', 'POSTS RECENTES <i class="fa fa-arrow-right"></i>', $pagination);
$pagination = str_replace('»', '<i class="fa fa-arrow-left"></i> MAIS POSTS', $pagination);
echo $pagination;
?>
This character «
(«) is the default left arrow that Laravel set to prev
and the other character »
(») the default to right.
Then just make a str_replace()
, replacing the arrow by whatever we want, a button, previous , next .
According to the documentation for Laravel 5.3 you can customize the view of the following form:
Running the artisan
command to carry the default view
of pagination to 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 the default pagination.
According to the documentation, you can customize your own features 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 pagination in the Laravel 5.2 and earlier
It is enough that instead of using the render()
method, do the following:
@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 release 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)