简体   繁体   English

如何同时使用分页和查找位置? (laravel 5.3)

[英]How to use paginate and findwhere at the same? (laravel 5.3)

I using third party. 我使用第三方。 I get from here : https://github.com/andersao/l5-repository 我从这里得到: https : //github.com/andersao/l5-repository

My function in file repository is like this : 我在文件存储库中的功能是这样的:

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;
}

When executed, there exist error like this : Method paginate does not exist. 执行时,会出现这样的错误: Method paginate does not exist. .

I try remove ->findWhere($clauses) . 我尝试删除->findWhere($clauses) It works. 有用。 No error. 没错

Findwhere and paginate apparently can not run simultaneously Findwhere和分页显然不能同时运行

Is there a solution to this problem? 有解决这个问题的方法吗?

It is because ->findWhere(...) ends the query with ->get() and returns the results (eloquent collection). 这是因为->findWhere(...) ->get()结束查询并返回结果(有说服力的集合)。

What you're trying to do is calling a ->paginate(...) method on an eloquent collection . 您要尝试的是在雄辩的collection上调用->paginate(...)方法。 It is actually a method on query builder . 它实际上是查询生成器上的一种方法。

Solution

applyConditions applyConditions

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

Edit 编辑

Okay, so ->applyCondtions() does not return the object so you can't chain methods off of it. 好的,所以->applyCondtions()不会返回对象,因此您不能将方法链接到该对象之外。 Try this: 尝试这个:

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

Like this: 像这样:

$list = $this->repository->scopeQuery(function($query) use ($filterColumns) {

    return $query->where($filterColumns)->orderBy('created_at', 'DESC');

});

$list->paginate(10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM