How to display hard coded pages links dynamically when customizing laravel pagination

I’m not sure if I’ve phrased my question properly but I’m customizing pagination and so far I have this in my view and it’s working well.

@if($paginator->hasPages())
<div class="pagination">
    @if ($paginator->currentPage() > 1)
    <a href="">&laquo;</a>
    @else
    <a href="" style="opacity:0.5; pointer-events:none;">&laquo;</a>
    @endif


    <a class="active" href="">1</a>
    <a href="">2</a>
    <a href="">3</a>
    <a href="">4</a>
    <a href="">5</a>


    @if (!$paginator->hasMorePages())
        <a href="" style="opacity:0.5; pointer-events:none;">&raquo;</a>
    @else
    <a href="">&raquo;</a>
    @endif

</div>
@endif

As you can see the pages links are hard coded. How do I display them dynamically?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try replacing all that with

@if($paginator->hasPages())
    {!! $paginator->render(); !!}
@endif

you can also
@if($paginator->hasPages())
<div class="pagination">
    @if ($paginator->currentPage() > 1)
    <a href="{{$paginator->previousPageUrl()}}">&laquo;</a>
    @else
    <a href="{{$paginator->url(1)}}" style="opacity:0.5; pointer-events:none;">&laquo;</a>
    @endif

    @if($paginator->currentPage()-2 >0)
    <a href="{{$paginator->url($paginator->currentPage()-2)}}">{{$paginator->currentPage()-2}}</a>
    @endif
    @if($paginator->currentPage()-1 >0)
    <a href="{{$paginator->url($paginator->currentPage()-1)}}">{{$paginator->currentPage()-1}}</a>
    @endif
    <a class="active" href="#">{{$paginator->currentPage()}}</a>
    @if($paginator->currentPage()+1 <=$paginator->lastPage())
    <a href="{{$paginator->url($paginator->currentPage()+1)}}">{{$paginator->currentPage()+1}}</a>
    @endif
    @if($paginator->currentPage()+2 <=$paginator->lastPage())
    <a href="{{$paginator->url($paginator->currentPage()+2)}}">{{$paginator->currentPage()+2}}</a>
    @endif



    @if (!$paginator->hasMorePages())
        <a href="{{$paginator->url($paginator->lastPage())}}" style="opacity:0.5; pointer-events:none;">&raquo;</a>
    @else
    <a href="{{$paginator->nextPageUrl()}}">&raquo;</a>
    @endif

</div>
@endif

This code is not clean 100% but you get the idea (especialy aroud the $paginator->hasMorePages()).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x