【问题标题】:How to use paginate and findwhere at the same? (laravel 5.3)如何同时使用 paginate 和 findwhere? (laravel 5.3)
【发布时间】:2017-01-19 21:54:36
【问题描述】:

我使用第三方。我从这里得到:https://github.com/andersao/l5-repository

我在文件存储库中的功能是这样的:

public function displayList($year, $id = NULL)
{
    $clauses = ['year' => $year];
    if(isset($id)) {
        $clauses = array_merge($clauses, ['id' => $id]);
    }
    $query = Self::orderBy('programcode')
                 ->orderBy('accountcode')
                 ->findWhere($clauses)
                 ->paginate(1);
    return $query;
}

执行时,出现如下错误:Method paginate does not exist.

我尝试删除->findWhere($clauses)。有用。没有错误。

Findwhere 和 paginate 显然不能同时运行

这个问题有解决办法吗?

【问题讨论】:

    标签: php laravel repository laravel-5.3


    【解决方案1】:

    像这样:

    $list = $this->repository->scopeQuery(function($query) use ($filterColumns) {
    
        return $query->where($filterColumns)->orderBy('created_at', 'DESC');
    
    });
    
    $list->paginate(10);
    

    【讨论】:

      【解决方案2】:

      这是因为->findWhere(...)结束查询with ->get()并返回结果(雄辩的集合)。

      您要做的是在eloquent collection 上调用->paginate(...) 方法。其实是query builder上的一个方法。

      解决方案

      applyConditions

      $query = Self::orderBy('programcode')
                   ->orderBy('accountcode')
                   ->applyConditions($clauses)
                   ->paginate(1);
      

      编辑

      好的,所以->applyCondtions() 不会返回对象,因此您不能将方法链接到它之外。试试这个:

      $query = Self::orderBy('programcode')
          ->orderBy('accountcode');
      $query->applyConditions($clauses);
      $query = $query->paginate(1);
      

      【讨论】:

      • 继续它的解决方法是什么?在文档中存在->paginate(...)。看这里:github.com/andersao/l5-repository
      • @mosestoh 在四处挖掘之后我很确定我找到了解决方案。更新了答案。
      • 存在错误:Call to a member function paginate() on null
      • @mosestoh 我已经更新了答案,你可以试试新方法吗?
      猜你喜欢
      • 1970-01-01
      • 2022-08-04
      • 2015-05-31
      • 2020-03-18
      • 2020-08-24
      • 2017-05-05
      • 2014-09-21
      • 2019-03-22
      • 2017-06-28
      相关资源
      最近更新 更多