dongtui0650 2017-01-19 21:54
浏览 391
已采纳

如何使用paginate和findwhere相同? (laravel 5.3)

I using third party. I get from here : 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..

I try remove ->findWhere($clauses). It works. No error.

Findwhere and paginate apparently can not run simultaneously

Is there a solution to this problem?

  • 写回答

2条回答 默认 最新

  • dongxiang5879 2017-01-19 23:33
    关注

    It is because ->findWhere(...) ends the query with ->get() and returns the results (eloquent collection).

    What you're trying to do is calling a ->paginate(...) method on an eloquent collection. It is actually a method on query builder.

    Solution

    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. Try this:

    $query = Self::orderBy('programcode')
        ->orderBy('accountcode');
    $query->applyConditions($clauses);
    $query = $query->paginate(1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?