简体   繁体   中英

In Laravel Pagination how to add first page and last page?

In Laravel 8 pagination how to show less number of pages and also how to add "First Page" and "Last Page"

Now in Laravel when we have more data, it will show the pagination like this: Laravel 8 分页

But I want my pagination to be like the images given below:

所需的分页样本

Or

所需的分页样本

To customizing the pagination view, run below command in console

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

in /resources/views/vendor/pagination/bootstrap-4.blade.php add bellow code.

<ul class="pagination">
    @if ($paginator->onFirstPage())
        <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.first')">
            <span class="page-link" aria-hidden="true">&laquo;</span>
        </li>
    @else
        <li class="page-item">
            <a class="page-link" href="{{ \Request::url() }}" rel="prev" aria-label="@lang('pagination.first')">&laquo;</a>
        </li>
    @endif

    ...

    @if ($paginator->hasMorePages())
        <li class="page-item">
            <a class="page-link" href="{{ \Request::url().'?page='.$paginator->lastPage() }}" rel="last" aria-label="@lang('pagination.last')">&raquo;</a>
        </li>
    @else
        <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.last')">
            <span class="page-link" aria-hidden="true">&raquo;</span>
        </li>
    @endif
</ul>
<?php 

class MyPresenter extends Illuminate\Pagination\BootstrapPresenter {

    public function render()
    {
        if ($this->currentPage == 1) {
            $content = $this->getPageRange(1, $this->currentPage + 2); 
        }
        else if ($this->currentPage >= $this->lastPage) {
            $content = $this->getPageRange($this->currentPage - 2, $this->lastPage); 
        }
        else {
            $content = $this->getPageRange($this->currentPage - 1, $this->currentPage + 1); 
        }

        return $this->getFirst().$this->getPrevious('&lsaquo;').$content.$this->getNext('&rsaquo;').$this->getLast();
    }

    public function getFirst($text = '&laquo;')
    {
        if ($this->currentPage <= 1)
        {
            return $this->getDisabledTextWrapper($text);
        }
        else
        {
            $url = $this->paginator->getUrl(1);
            return $this->getPageLinkWrapper($url, $text);
        }
    }

    public function getLast($text = '&raquo;')
    {
        if ($this->currentPage >= $this->lastPage)
        {
            return $this->getDisabledTextWrapper($text);
        }
        else
        {
            $url = $this->paginator->getUrl($this->lastPage);

            return $this->getPageLinkWrapper($url, $text);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM