paginator call on query builder does not return paginator object in laravel 4 (in a specific case)

ぃ、小莉子 提交于 2019-12-12 05:18:44

问题


Problem:

Query builder:

$r = DB::table('someTable');
$r->where(....)
$r->paginate(30, array(....))
return $r;

Now calling a get_class() on $r gives Illuminate\Database\Query\Builder

But after opening up vendor/laravel/framework/src/Illuminate/Database/Query/builder.php, i saw this,

public function paginate($perPage = 15, $columns = array('*'))
{
    $paginator = $this->connection->getPaginator();


    if (isset($this->groups))
    {
        return $this->groupedPaginate($paginator, $perPage, $columns);
    }
    else
    {            
        return $this->ungroupedPaginate($paginator, $perPage, $columns);
    }
}

The line above, return $this->ungroupedPaginate($paginator, $perPage, $columns); indeed returns a Illuminate\Pagination\Paginator object.

But after that, it again becomes the query builder object.

However,

if i do a continuous chain, like below

$r = DB::table('someTable')->where(...)->orderBy(....)->paginate(....)

It returns a Paginator object.

In both the above two cases, calling toSql() returns the same sql which is being run. So builder is construting the same query only returning different objects in above two cases.

What may be the cause of this? because in my opinion, the resulting object in both the above two cases should be same which is Paginator. or am i missing something basic here?


回答1:


You need to re-assign the returned value to the $r variable.

$r = DB::table('someTable');
$r = $r->where(....)
$r = $r->paginate(30, array(....))
return $r;


来源:https://stackoverflow.com/questions/17455712/paginator-call-on-query-builder-does-not-return-paginator-object-in-laravel-4-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!