Kareimovich's avatar

laravel pagination get first page

Hi, everybody, I need to add First-page url like my last page Wehn i use firstItem i don't get full url

@if ($paginator->hasPages())
    <ul class="pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
         <li class="disabled"><span>&laquo;</span></li>
           @else
       <li><a href="{{ $paginator->url($page)  }}" rel="prev">First</a></li>

        <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">&laquo;</a></li>
        
     @endif

        {{-- Pagination Elements --}}
        @foreach ($elements as $element)
                {{-- "Three Dots" Separator --}}
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
         @endif

        {{-- Array Of Links --}}
        @if (is_array($element))
            @foreach ($element as $page => $url)
                @if ($page == $paginator->currentPage())
                    <li class="active"><span>{{ $page }}</span></li>
                 @else
                     <li><a href="{{ $url }}">{{ $page }}</a></li>
                 @endif
             @endforeach
            @endif
     @endforeach

        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">&raquo;</a></li>
        <li><a href="{{ $paginator->url($page) }}" rel="next">Last</a></li>

     @else
         <li class="disabled"><span>&raquo;</span></li>
     @endif
    </ul>
@endif
0 likes
8 replies
deansatch's avatar

Is there a reason you are manually building this instead of just {{ $elements->links() }}

1 like
Kareimovich's avatar

@deansatch yes i want to show first and last page because i have more than 30 pages it will be boring for users

deansatch's avatar

You still could have used that to generate your page numbers, next and previous..then you would just need to add your own first and last page links either side

Wouldn't the first page link just be the plain route with no query string at all?

deansatch's avatar

Like I said, why wouldn't the first page url not just be the plain route? No calculations needed


<li><a href="{{ route('yourRoute')  }}">First</a></li>

Kareimovich's avatar

@deansatch the route will be diffrent i have lot of page cant do this although i gat Route [] not defined. please don't forget i'm working on views\vendor\pagination\default.blade.php not controller or routes i need to get first page url from elements or revers url($page)

deansatch's avatar
Level 24

In that case you can access everything from the toArray() method:


<li><a href="{{ $paginator->toArray()['first_page_url'] }}">First</a></li>

2 likes

Please or to participate in this conversation.