Skip to content

Commit

Permalink
Merge branch 'master' of github.com:marcelgwerder/laravel-api-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Gwerder committed Feb 7, 2017
2 parents cd1c8cf + 254c394 commit eeac90d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/CountMetaProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Marcelgwerder\ApiHandler;

use Illuminate\Support\Facades\DB;

class CountMetaProvider extends MetaProvider
{
/**
Expand Down Expand Up @@ -28,6 +30,17 @@ public function __construct($title, $builder)
*/
public function get()
{
if (!empty($this->builder->groups)) {
//Only a count column is required
$this->builder->columns = [];
$this->builder->selectRaw('count(*) as aggregate');
$this->builder->limit = null;

//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
$query = DB::query()->selectRaw('count(*) as aggregate from (' . $this->builder->toSql() . ') as count_table', $this->builder->getBindings());
return intval($query->first()->aggregate);
}

return intval($this->builder->count());
}
}

0 comments on commit eeac90d

Please sign in to comment.