问题
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