php 如何在 Laravel 5 中使用分页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27980373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:39:03  来源:igfitidea点击:

How to use pagination in laravel 5?

phplaravelpaginationlaravel-5

提问by Sojan V Jose

am trying to port my laravel4 application to laravel 5 . In the previous version i could use the following method for generating pagination urls .

我正在尝试将我的 laravel4 应用程序移植到 laravel 5。在以前的版本中,我可以使用以下方法生成分页网址。

In controller:

在控制器中

$this->data['pages']= Page::whereIn('area_id', $suburbs)->where('score','>','0')->orderBy('score','desc')->paginate(12);

and after sharing the data array with the view, i could user

在与视图共享数据数组后,我可以使用

In views:

在视图中

{{$pages->links()}}

In laravel 5 the doing that results in following error

在 Laravel 5 中,这样做会导致以下错误

ErrorException in AbstractPaginator.php line 445:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'links'

not sure what i am missing here, can somebody help ?

不确定我在这里缺少什么,有人可以帮忙吗?

回答by Alasu Paul Sabrin

In Laravel 5 there is no method "links" you can try this

在 Laravel 5 中没有方法“链接”你可以试试这个

{!! $pages->render() !!}

回答by David

In other frameworks, pagination can be very painful. Laravel 5 makes it a breeze. For using it, first you have to make a change in your controller code where you calling data from the database:

在其他框架中,分页可能非常痛苦。Laravel 5 让它变得轻而易举。要使用它,首先您必须更改从数据库调用数据的控制器代码:

 public function index()
{       
        $users = DB::table('books')->simplePaginate(5);
        //using pagination method
        return view('index', ['users' => $users]);
}

...after that you can use this code:

...之后,您可以使用此代码:

<?php echo $users->render(); ?>

That will make you use simple Laravel 5 beauty.

这将使您使用简单的 Laravel 5 美。

回答by Nadeem Qasmi

Pagination Laravel 5.6.26, for the pagination the controller are :

分页 Laravel 5.6.26,对于分页,控制器是:

Controller code (https://laravel.com/docs/5.6/pagination#basic-usage)

控制器代码(https://laravel.com/docs/5.6/pagination#basic-usage

    posts = Post::orderBy('created_at','desc')->paginate(10);
    return view('posts.index')->with('posts', $posts);

Front end into blade (view) (https://laravel.com/docs/5.6/pagination#displaying-pagination-results)

前端进入刀片(视图)(https://laravel.com/docs/5.6/pagination#displaying-pagination-results

   {{ $users->links() }}

回答by Jose Mhlanga

$items = SomeDataModel->get()->paginate(2); // in your controller

@foreach(items as $item)   // in your view.blade file
....echo some data here
@endforeach

<div class="pagination">
    {{ $items->render() }} or {{ $items->links() }}
</div>

Use the array name (items) in the render() or links() method NOT the array item. It worked for me.

在 render() 或 links() 方法中使用数组名称(项目)而不是数组项目。它对我有用。

回答by Rahul Saha

@if ($posts->lastPage() > 1)
        <nav aria-label="Page navigation">
            <ul class="pagination">
                @if($posts->currentPage() != 1 && $posts->lastPage() >= 5)
                <li>
                    <a href="{{ $posts->url($posts->url(1)) }}" aria-label="Previous">
                        <span aria-hidden="true">First</span>
                    </a>
                </li>
                @endif
                @if($posts->currentPage() != 1)
                <li>
                    <a href="{{ $posts->url($posts->currentPage()-1) }}" aria-label="Previous">
                        <span aria-hidden="true">&#x3C;</span>
                    </a>
                </li>
                @endif
                @for($i = max($posts->currentPage()-2, 1); $i <= min(max($posts->currentPage()-2, 1)+4,$posts->lastPage()); $i++)
                @if($posts->currentPage() == $i)
                <li class="active">
                @else
                <li>
                @endif
                    <a href="{{ $posts->url($i) }}">{{ $i }}</a>
                </li>
                @endfor
                @if ($posts->currentPage() != $posts->lastPage())
                <li>
                    <a href="{{ $posts->url($posts->currentPage()+1) }}" aria-label="Next">
                        <span aria-hidden="true">&#x3E;</span>
                    </a>
                </li>
                @endif
                @if ($posts->currentPage() != $posts->lastPage() && $posts->lastPage() >= 5)
                <li>
                    <a href="{{ $posts->url($posts->lastPage()) }}" aria-label="Next">
                        <span aria-hidden="true">Last</span>
                    </a>
                </li>
                @endif
            </ul>
        </nav>
        @endif

回答by diego

Hi there is my code for pagination: Use in view:
@include('pagination.default', ['paginator' => $users])

嗨,我的分页代码:在视图中使用:
@include('pagination.default', ['paginator' => $users])

Views/pagination/default.blade.php

视图/分页/default.blade.php

@if ($paginator->lastPage() > 1)
    <ul class="pagination">
        <!-- si la pagina actual es distinto a 1 y hay mas de 5 hojas muestro el boton de 1era hoja -->
        <!-- if actual page is not equals 1, and there is more than 5 pages then I show first page button -->
        @if ($paginator->currentPage() != 1 && $paginator->lastPage() >= 5)
            <li>
                <a href="{{ $paginator->url($paginator->url(1)) }}" >
                    <<
                </a>
            </li>
        @endif

        <!-- si la pagina actual es distinto a 1 muestra el boton de atras -->
        @if($paginator->currentPage() != 1)
            <li>
                <a href="{{ $paginator->url($paginator->currentPage()-1) }}" >
                    <
                </a>
            </li>
        @endif

        <!-- dibuja las hojas... Tomando un rango de 5 hojas, siempre que puede muestra 2 hojas hacia atras y 2 hacia adelante -->
        <!-- I draw the pages... I show 2 pages back and 2 pages forward -->
        @for($i = max($paginator->currentPage()-2, 1); $i <= min(max($paginator->currentPage()-2, 1)+4,$paginator->lastPage()); $i++)
                <li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
                    <a href="{{ $paginator->url($i) }}">{{ $i }}</a>
                </li>
        @endfor

        <!-- si la pagina actual es distinto a la ultima muestra el boton de adelante -->
        <!-- if actual page is not equal last page then I show the forward button-->
        @if ($paginator->currentPage() != $paginator->lastPage())
            <li>
                <a href="{{ $paginator->url($paginator->currentPage()+1) }}" >
                    >
                </a>
            </li>
        @endif

        <!-- si la pagina actual es distinto a la ultima y hay mas de 5 hojas muestra el boton de ultima hoja -->
        <!-- if actual page is not equal last page, and there is more than 5 pages then I show last page button -->
        @if ($paginator->currentPage() != $paginator->lastPage() && $paginator->lastPage() >= 5)
            <li>
                <a href="{{ $paginator->url($paginator->lastPage()) }}" >
                    >>
                </a>
            </li>
        @endif
    </ul>
@endif