You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/CountMetaProvider.php
+18-2Lines changed: 18 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,8 @@
1
-
<?phpnamespaceMarcelgwerder\ApiHandler;
1
+
<?php
2
+
3
+
namespaceMarcelgwerder\ApiHandler;
4
+
5
+
use \Illuminate\Database\Query\BuilderasQueryBuilder;
2
6
3
7
class CountMetaProvider extends MetaProvider
4
8
{
@@ -11,7 +15,18 @@ class CountMetaProvider extends MetaProvider
11
15
12
16
publicfunction__construct($title, $builder)
13
17
{
14
-
$this->builder = clone$builder;
18
+
$builder = clone$builder;
19
+
20
+
if($builderinstanceof QueryBuilder) {
21
+
$this->builder = $builder;
22
+
} elseif(method_exists($builder, 'toBase')) {
23
+
// Laravel > 5.2 as global scopes, toBase makes sure they are included.
24
+
$this->builder = $builder->toBase();
25
+
} else {
26
+
// Laravel < 5.2 did not have global scopes and thus the query builder is enough.
27
+
$this->builder = $builder->getQuery();
28
+
}
29
+
15
30
$this->title = $title;
16
31
17
32
//Remove offset from builder because a count doesn't work in combination with an offset
@@ -36,6 +51,7 @@ public function get()
36
51
37
52
//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
38
53
$query = $this->builder->newQuery()->selectRaw('count(*) as aggregate from (' . $this->builder->toSql() . ') as count_table', $this->builder->getBindings());
0 commit comments