2015-01-16 87 views
22

正试图将我的laravel4应用程序移植到laravel 5。在以前的版本中,我可以使用以下方法来生成分页url。如何在laravel 5中使用分页?

在控制器

$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稳定,因为它有一些bug并且基本代码可以改变。 –

+0

@ClaudioLudovicoPanetta我想这个月会出来:)。只是想与变化同步。使用4.2生产虽然。 –

+1

你的问题似乎比你的问题更多的Laravel问题,我会尝试复制你的问题的解决方案,brb。 –

回答

56

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

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

{!! $ pages-> render()!!}做了窍门,laravel5中的其他语法转义html。谢谢 –

+0

最后一个简单的分页解决方案,而不是这个新的“创建5个php文件,实现一个类并编写200行代码”。最后!谢谢。 – hfingler

+0

这不起作用。在使用之前我有什么需要准备的? – Muhaimin

-5

你好是我的分页代码:鉴于 用途:
@include( 'pagination.default',[ '分页程序'=> $用户])

查看/分页/ 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 
+0

看,我没有复制好......代码在灰色区域之前开始。 – diego

+1

你可以随时[编辑](http://stackoverflow.com/posts/30744604/edit)你的代码! – ZygD

4

在其它框架,分页可为v疼痛难忍。 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美人。

-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 
+0

给出一些解释,答案应该至少解释一下。 –