【问题标题】:How to use pagination in laravel 5?如何在 laravel 5 中使用分页?
【发布时间】:2015-03-14 20:18:30
【问题描述】:

我正在尝试将我的 laravel4 应用程序移植到 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'

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

【问题讨论】:

  • 我建议等到 Laravel 5 稳定后,因为它有一些 bug 并且基础代码可以更改。
  • @ClaudioLudovicoPanetta 我想它会在这个月问世:)。只是想与更改同步。虽然使用 4.2 进行生产。
  • 您的问题似乎比您的问题更像是 Laravel 问题,我会尝试复制您的问题以获得解决方案,brb。
  • @ClaudioLudovicoPanetta 谢谢 :) 。将拭目以待。否则将在 github 上添加一个问题

标签: php laravel pagination laravel-5


【解决方案1】:

最佳方式分页和简单代码

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


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

【讨论】:

    【解决方案2】:

    使用

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

    在刀片中使用

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

    【讨论】:

      【解决方案3】:
      $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() 方法中使用数组名称(项)而不是数组项。它对我有用。

      【讨论】:

        【解决方案4】:

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

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

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

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

           {{ $users->links() }}
        

        【讨论】:

          【解决方案5】:
          @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
          

          【讨论】:

          • 给出一些解释,答案至少应该解释一下。
          【解决方案6】:

          在其他框架中,分页可能非常痛苦。 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 美感。

          【讨论】:

            【解决方案7】:

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

            视图/分页/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
            

            【讨论】:

            • 看我没有复制好...代码在灰色区域之前开始。
            【解决方案8】:

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

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

            【讨论】:

            • {!!$pages->render()!!} 成功了,其他语法在 laravel5 中转义了 html。谢谢
            • 终于解决了一个简单的分页,而不是这个新的“创建5个php文件,实现一个类,写200行代码”。最后!谢谢。
            • 这不起作用。使用前有什么需要先准备的吗?
            • @muhaimin 从数据库访问数据时,您需要附加 paginate() 函数。例如:Page::whereIn('area_id', $suburbs)-&gt;orderBy('score','desc')-&gt;paginate(12); 并将变量传递给视图。
            猜你喜欢
            • 2016-04-07
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            • 2015-07-31
            • 2016-06-24
            • 2017-11-30
            • 1970-01-01
            • 2017-03-04
            相关资源
            最近更新 更多