Skip to content

Commit 5050195

Browse files
[10.x] Add whereAll and whereAny methods to the query builder (#50344)
* Add "whereMultiple" method to the Query Builder * Add a test for the "whereMultiple" method * Add a missing argument to 'assertSame' in the test * Add new checks to test the method with different arguments * Fix preparing of columnsBoolean and operator arguments * Convert operators in the test to lowercase * Trigger tests * Fix formatting (StyleCI) * Split 'whereMultiple' method to whereAll and whereAny * Add tests for orWhereAll and orWhereAny methods * Remove flatting value from 'whereMultiple' method * Remove 'whereMultiple' method * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent d7235b3 commit 5050195

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,80 @@ public function orWhereFullText($columns, $value, array $options = [])
20952095
return $this->whereFulltext($columns, $value, $options, 'or');
20962096
}
20972097

2098+
/**
2099+
* Add a "where" clause to the query for multiple columns with "and" conditions between them.
2100+
*
2101+
* @param string[] $columns
2102+
* @param mixed $operator
2103+
* @param mixed $value
2104+
* @param string $boolean
2105+
* @return $this
2106+
*/
2107+
public function whereAll($columns, $operator = null, $value = null, $boolean = 'and')
2108+
{
2109+
[$value, $operator] = $this->prepareValueAndOperator(
2110+
$value, $operator, func_num_args() === 2
2111+
);
2112+
2113+
$this->whereNested(function ($query) use ($columns, $operator, $value) {
2114+
foreach ($columns as $column) {
2115+
$query->where($column, $operator, $value, 'and');
2116+
}
2117+
}, $boolean);
2118+
2119+
return $this;
2120+
}
2121+
2122+
/**
2123+
* Add an "or where" clause to the query for multiple columns with "and" conditions between them.
2124+
*
2125+
* @param string[] $columns
2126+
* @param string $operator
2127+
* @param mixed $value
2128+
* @return $this
2129+
*/
2130+
public function orWhereAll($columns, $operator = null, $value = null)
2131+
{
2132+
return $this->whereAll($columns, $operator, $value, 'or');
2133+
}
2134+
2135+
/**
2136+
* Add an "where" clause to the query for multiple columns with "or" conditions between them.
2137+
*
2138+
* @param string[] $columns
2139+
* @param string $operator
2140+
* @param mixed $value
2141+
* @param string $boolean
2142+
* @return $this
2143+
*/
2144+
public function whereAny($columns, $operator = null, $value = null, $boolean = 'and')
2145+
{
2146+
[$value, $operator] = $this->prepareValueAndOperator(
2147+
$value, $operator, func_num_args() === 2
2148+
);
2149+
2150+
$this->whereNested(function ($query) use ($columns, $operator, $value) {
2151+
foreach ($columns as $column) {
2152+
$query->where($column, $operator, $value, 'or');
2153+
}
2154+
}, $boolean);
2155+
2156+
return $this;
2157+
}
2158+
2159+
/**
2160+
* Add an "or where" clause to the query for multiple columns with "or" conditions between them.
2161+
*
2162+
* @param string[] $columns
2163+
* @param string $operator
2164+
* @param mixed $value
2165+
* @return $this
2166+
*/
2167+
public function orWhereAny($columns, $operator = null, $value = null)
2168+
{
2169+
return $this->whereAny($columns, $operator, $value, 'or');
2170+
}
2171+
20982172
/**
20992173
* Add a "group by" clause to the query.
21002174
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,68 @@ public function testWhereFulltextPostgres()
11851185
$this->assertEquals(['Car Plane'], $builder->getBindings());
11861186
}
11871187

1188+
public function testWhereAll()
1189+
{
1190+
$builder = $this->getBuilder();
1191+
$builder->select('*')->from('users')->whereAll(['last_name', 'email'], '%Otwell%');
1192+
$this->assertSame('select * from "users" where ("last_name" = ? and "email" = ?)', $builder->toSql());
1193+
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
1194+
1195+
$builder = $this->getBuilder();
1196+
$builder->select('*')->from('users')->whereAll(['last_name', 'email'], 'not like', '%Otwell%');
1197+
$this->assertSame('select * from "users" where ("last_name" not like ? and "email" not like ?)', $builder->toSql());
1198+
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
1199+
}
1200+
1201+
public function testOrWhereAll()
1202+
{
1203+
$builder = $this->getBuilder();
1204+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAll(['last_name', 'email'], 'like', '%Otwell%');
1205+
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? and "email" like ?)', $builder->toSql());
1206+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1207+
1208+
$builder = $this->getBuilder();
1209+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereAll(['last_name', 'email'], 'like', '%Otwell%', 'or');
1210+
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? and "email" like ?)', $builder->toSql());
1211+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1212+
1213+
$builder = $this->getBuilder();
1214+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAll(['last_name', 'email'], '%Otwell%');
1215+
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" = ? and "email" = ?)', $builder->toSql());
1216+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1217+
}
1218+
1219+
public function testWhereAny()
1220+
{
1221+
$builder = $this->getBuilder();
1222+
$builder->select('*')->from('users')->whereAny(['last_name', 'email'], 'like', '%Otwell%');
1223+
$this->assertSame('select * from "users" where ("last_name" like ? or "email" like ?)', $builder->toSql());
1224+
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
1225+
1226+
$builder = $this->getBuilder();
1227+
$builder->select('*')->from('users')->whereAny(['last_name', 'email'], '%Otwell%');
1228+
$this->assertSame('select * from "users" where ("last_name" = ? or "email" = ?)', $builder->toSql());
1229+
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
1230+
}
1231+
1232+
public function testOrWhereAny()
1233+
{
1234+
$builder = $this->getBuilder();
1235+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAny(['last_name', 'email'], 'like', '%Otwell%');
1236+
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? or "email" like ?)', $builder->toSql());
1237+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1238+
1239+
$builder = $this->getBuilder();
1240+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereAny(['last_name', 'email'], 'like', '%Otwell%', 'or');
1241+
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? or "email" like ?)', $builder->toSql());
1242+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1243+
1244+
$builder = $this->getBuilder();
1245+
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAny(['last_name', 'email'], '%Otwell%');
1246+
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" = ? or "email" = ?)', $builder->toSql());
1247+
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
1248+
}
1249+
11881250
public function testUnions()
11891251
{
11901252
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)