Skip to content

Commit eeac90d

Browse files
author
Marcel Gwerder
committed
Merge branch 'master' of github.com:marcelgwerder/laravel-api-handler
2 parents cd1c8cf + 254c394 commit eeac90d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/CountMetaProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Marcelgwerder\ApiHandler;
22

3+
use Illuminate\Support\Facades\DB;
4+
35
class CountMetaProvider extends MetaProvider
46
{
57
/**
@@ -28,6 +30,17 @@ public function __construct($title, $builder)
2830
*/
2931
public function get()
3032
{
33+
if (!empty($this->builder->groups)) {
34+
//Only a count column is required
35+
$this->builder->columns = [];
36+
$this->builder->selectRaw('count(*) as aggregate');
37+
$this->builder->limit = null;
38+
39+
//Use the original builder as a subquery and count over it because counts over groups return the number of rows for each group, not for the total results
40+
$query = DB::query()->selectRaw('count(*) as aggregate from (' . $this->builder->toSql() . ') as count_table', $this->builder->getBindings());
41+
return intval($query->first()->aggregate);
42+
}
43+
3144
return intval($this->builder->count());
3245
}
3346
}

0 commit comments

Comments
 (0)