Skip to content

Commit 1e9b31d

Browse files
Merge pull request #52 from atapatel/main
Fix union query issue
2 parents 568bde5 + e07223a commit 1e9b31d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/FastPaginate.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ protected function paginate(string $paginationMethod, Closure $paginatorOutput)
4343
) {
4444
/** @var \Illuminate\Database\Query\Builder $this */
4545
$base = $this->getQuery();
46-
// Havings and groups don't work well with this paradigm, because we are
47-
// counting on each row of the inner query to return a primary key
46+
// Havings, groups, and unions don't work well with this paradigm, because
47+
// we are counting on each row of the inner query to return a primary key
4848
// that we can use. When grouping, that's not always the case.
49-
if (filled($base->havings) || filled($base->groups)) {
49+
if (filled($base->havings) || filled($base->groups) || filled($base->unions)) {
5050
return $this->{$paginationMethod}($perPage, $columns, $pageName, $page);
5151
}
5252

@@ -99,6 +99,7 @@ protected function paginate(string $paginationMethod, Closure $paginatorOutput)
9999
}
100100

101101
/**
102+
* @param $builder
102103
* @return array
103104
*
104105
* @throws QueryIncompatibleWithFastPagination

tests/Integration/BuilderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,19 @@ public function with_sum_has_the_correct_number_of_parameters()
408408

409409
$this->assertEquals(get_class($fast), get_class($regular));
410410
}
411+
412+
public function test_for_union_query()
413+
{
414+
$queries = $this->withQueriesLogged(function () use (&$results) {
415+
$results = User::where('id', '<', 10)
416+
->unionAll(User::where('id', '>', 10))
417+
->fastPaginate(2);
418+
});
419+
420+
$this->assertEquals($queries[0]['query'],
421+
'select count(*) as aggregate from ((select * from `users` where `id` < ?) union all (select * from `users` where `id` > ?)) as `temp_table`');
422+
423+
$this->assertEquals($queries[1]['query'],
424+
'(select * from `users` where `id` < ?) union all (select * from `users` where `id` > ?) limit 2 offset 0');
425+
}
411426
}

0 commit comments

Comments
 (0)