【问题标题】:Laravel Method paginate does not existLaravel 方法分页不存在
【发布时间】:2018-06-17 07:46:55
【问题描述】:

我正在尝试对模型结果进行分页,但我收到“方法分页不存在。”。这是我的代码:

$user_dispatches = Dispatch::all()->where('user_id', Auth::id())->paginate(10);

我需要获取用户 ID 等于当前经过身份验证的用户 ID 的所有记录。没有 paginate() 方法也能正常工作。

【问题讨论】:

    标签: laravel methods pagination


    【解决方案1】:

    扩展一点 Alexey 的完美答案:

    Dispatch::all() => 返回Collection

    Dispatch::all()->where() => 返回Collection

    Dispatch::where() => 返回Query

    Dispatch::where()->get() => 返回Collection

    Dispatch::where()->get()->where() => 返回Collection

    您只能在Query 上调用“paginate”,而不能在Collection 上调用。

    是的,同时为QueriesCollections 设置一个where 函数是完全令人困惑的,它们的工作方式与它们一样接近,但它就是这样。

    【讨论】:

    • 很好地解释了。应标记为正确答案
    【解决方案2】:

    你需要删除all():

    Dispatch::where('user_id', Auth::id())->paginate(10);
    

    当您使用all() 时,您会从表中获取所有行并获取一个集合。然后您使用集合方法where()(而不是查询生成器方法where()),然后您尝试在集合上使用paginate() 方法,但它不存在。

    【讨论】:

    • 完美解释@Alexy Mezenin
    【解决方案3】:

    要使用所有记录和分页,您需要使用以下代码:

    $user_dispatches = Disspath::paginate(8);
    

    【讨论】:

      【解决方案4】:

      你需要删除方法all()

      $user_dispatches = Dispatch::where('user_id', Auth::id())->paginate(10);
      

      因为all() 返回Collectionpaginate() 使用Builder

      【讨论】:

        【解决方案5】:
        Dispatch::where('user_id', auth()->user()->id)->paginate(10);
        

        【讨论】:

          【解决方案6】:

          您可以创建自己的自定义类:

          <?php
          namespace App\CustomClasses;
          
          use Illuminate\Container\Container;
          use Illuminate\Pagination\LengthAwarePaginator;
          use Illuminate\Pagination\Paginator;
          use Illuminate\Support\Collection;
          
          class ColectionPaginate
          {
              public static function paginate(Collection $results, $pageSize)
              {
                  $page = Paginator::resolveCurrentPage('page');
                  
                  $total = $results->count();
          
                  return self::paginator($results->forPage($page, $pageSize), $total, $pageSize, $page, [
                      'path' => Paginator::resolveCurrentPath(),
                      'pageName' => 'page',
                  ]);
          
              }
          
              /**
               * Create a new length-aware paginator instance.
               *
               * @param  \Illuminate\Support\Collection  $items
               * @param  int  $total
               * @param  int  $perPage
               * @param  int  $currentPage
               * @param  array  $options
               * @return \Illuminate\Pagination\LengthAwarePaginator
               */
              protected static function paginator($items, $total, $perPage, $currentPage, $options)
              {
                  return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
                      'items', 'total', 'perPage', 'currentPage', 'options'
                  ));
              }
          }
          

          然后使用它:

          use App\CustomClasses\ColectionPaginate;
          ...
          $result = $query->limit(100)->get();
          $paginatedResult = ColectionPaginate::paginate($result, 10);
          

          【讨论】:

            猜你喜欢
            • 2016-07-14
            • 2018-06-19
            • 1970-01-01
            • 2019-03-06
            • 1970-01-01
            • 2019-10-02
            • 2017-07-11
            • 2016-11-05
            • 2021-02-28
            相关资源
            最近更新 更多