Reputation: 139
I'm pulling out the Eloquent module to use it separately, but I have access to the QueryBuilder functions. They're all working except for that one.
When I run a count() and a getPaginationCount(), the count() returns the correct value, but getPaginationCount() just returns the Illuminate\Database\Eloquent\Builder object, as if I didn't command a query to run. However, I can see 2 queries in the query log, and oddly, they both run the same query.
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => TECHDB_HOST,
'database' => TECHDB_DBNAME,
'username' => TECHDB_USER,
'password' => TECHDB_PASS,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
));
$capsule->bootEloquent();
class Indicator extends Illuminate\Database\Eloquent\Model {
public $timestamps = false;
public $table = "indicator_performance";
}
$query = Indicator::where('symbol', '=', 'IBM')->forPage(1,2);
var_dump($query->count()); //Correctly prints '13'
var_dump($query->getPaginationCount()); //dumps the 'Illuminate\Database\Eloquent\Builder' object
Here is the query log:
array (size=2)
0 =>
array (size=3)
'query' => string 'select count(*) as aggregate from `indicator_performance` where `symbol` = ? limit 2 offset 0' (length=93)
'bindings' =>
array (size=1)
0 => string 'IBM' (length=3)
'time' => float 4.85
1 =>
array (size=3)
'query' => string 'select count(*) as aggregate from `indicator_performance` where `symbol` = ? limit 2 offset 0' (length=93)
'bindings' =>
array (size=1)
0 => string 'IBM' (length=3)
'time' => float 4.24
EDIT:
It seems I've exposed a more general bug with the count()
function when used after a forPage()
call. Have a look at these results:
$query = Indicator::where('symbol', '=', 'IBM');
var_dump($query->count()); //Correctly returns '13'
var_dump($query->forPage(0, 5)->count()); //Correctly returns '13'
var_dump($query->forPage(1, 5)->count()); //Correctly returns '13'
var_dump($query->forPage(2, 5)->count()); //Returns null. Should return '13' or '3', depending on implementation.
Upvotes: 0
Views: 502
Reputation: 81187
There are 2 problems with your code (and the Laravel):
getPaginationCount()
called on the Eloquent Builder runs the method (on the Query Builder class) but doesn't return its result. Instead it returns itself as $this
.
getPaginationCount()
doesn't reset limit
and offset
on the query to get the count, which I suppose is a bug. This results in returning null
whenever offset is set to anything higher than 1. forPage($page)
will do that for $page > 1
.
That being said, I suggest you either use count()
instead of getPaginationCount()
or:
// first get the count
$count = $query->getQuery() // get the base Query Builder directly
->getPaginationCount(); // in order to return the count
// then use forPage
$query->forPage(1,5);
Upvotes: 1