Skip to content

Commit 0efbd7e

Browse files
authored
Document that union queries can be built using eloquent builders (#43949)
1 parent 7dd2905 commit 0efbd7e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,7 @@ protected function removeExistingOrdersFor($column)
24382438
/**
24392439
* Add a union statement to the query.
24402440
*
2441-
* @param \Illuminate\Database\Query\Builder|\Closure $query
2441+
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
24422442
* @param bool $all
24432443
* @return $this
24442444
*/
@@ -2458,7 +2458,7 @@ public function union($query, $all = false)
24582458
/**
24592459
* Add a union all statement to the query.
24602460
*
2461-
* @param \Illuminate\Database\Query\Builder|\Closure $query
2461+
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
24622462
* @return $this
24632463
*/
24642464
public function unionAll($query)

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,12 @@ public function testUnions()
993993
$builder->union($this->getSqlServerBuilder()->select('name')->from('users')->where('id', '=', 2));
994994
$this->assertEquals($expectedSql, $builder->toSql());
995995
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
996+
997+
$builder = $this->getBuilder();
998+
$eloquentBuilder = new EloquentBuilder($this->getBuilder());
999+
$builder->select('*')->from('users')->where('id', '=', 1)->union($eloquentBuilder->select('*')->from('users')->where('id', '=', 2));
1000+
$this->assertSame('(select * from "users" where "id" = ?) union (select * from "users" where "id" = ?)', $builder->toSql());
1001+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
9961002
}
9971003

9981004
public function testUnionAlls()
@@ -1009,6 +1015,13 @@ public function testUnionAlls()
10091015
$builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
10101016
$this->assertEquals($expectedSql, $builder->toSql());
10111017
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
1018+
1019+
$builder = $this->getBuilder();
1020+
$eloquentBuilder = new EloquentBuilder($this->getBuilder());
1021+
$builder->select('*')->from('users')->where('id', '=', 1);
1022+
$builder->unionAll($eloquentBuilder->select('*')->from('users')->where('id', '=', 2));
1023+
$this->assertSame('(select * from "users" where "id" = ?) union all (select * from "users" where "id" = ?)', $builder->toSql());
1024+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
10121025
}
10131026

10141027
public function testMultipleUnions()

0 commit comments

Comments
 (0)