如何在Laravel 5中使用分页功能?

34

我想将我的Laravel 4应用程序迁移到Laravel 5。在以前的版本中,我可以使用以下方法生成分页URL。

在控制器中:

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

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

在视图中:

{{$pages->links()}}

在 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'

不确定我在这里漏掉了什么,有人可以帮忙吗?


3
我建议等待 Laravel 5 稳定版本发布,因为它目前存在一些漏洞并且基础代码可能会发生变化。 - ExoticSeagull
@ClaudioLudovicoPanetta 我认为它将在本月发布 :)。只是想与变化保持同步。不过我们正在生产中使用4.2版本。 - Sojan Jose
1
你的问题似乎更像是 Laravel 的问题,而不是你自己的问题。我会尝试复制你的问题以找到解决方案,马上回来。 - ExoticSeagull
@ClaudioLudovicoPanetta 谢谢 :)。我会等待并观察。否则,我会在 GitHub 上添加一个问题。 - Sojan Jose
8个回答

73

Laravel 5中没有"links"方法,你可以尝试使用以下方法

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

13
"{!!$pages->render()!!}" 起了作用,另一种语法在 Laravel5 中会转义 HTML。谢谢。 - Sojan Jose
终于有一个简单的分页解决方案了,不是那种需要“创建5个PHP文件、实现一个类并编写200行代码”的新方法。终于!谢谢。 - hfingler
这个不起作用。在使用它之前,我需要准备什么吗? - Muhaimin
@muhaimin 在从数据库访问数据时,您需要附加paginate()函数。例如:Page::whereIn('area_id', $suburbs)->orderBy('score','desc')->paginate(12);并将变量传递到视图。 - VijayRana

9

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

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

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

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

那会让你更轻松地使用简单优美的Laravel 5。

6

5
$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>

在 render() 或 links() 方法中使用数组名称 (items),而不是数组项。这对我很有效。


2

最佳分页实现方式和简单代码

 $limit = 10;
 $page_no = ($request->input('**page_no**') * $limit) - $limit;


   $data = User::whereraw($whereraw)
                  ->limit(**$limit**)
                  ->offset(**$page_no**)
                  ->get();

1
使用
$users = User::where('id', Auth::user()->id)->simplePaginate(20);

并在Blade中使用

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

未定义变量: pages - Ahmed Numaan

-1
@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

给出一些解释,答案应该至少有一点解释。 - Akshay Bhat 'AB'

-7

这是我的分页代码:

在视图中使用:
@include('pagination.default', ['paginator' => $users])

Views/pagination/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

看,我复制得不好...代码从灰色区域之前开始。 - diego

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接