Skip to content

Commit dd83c6c

Browse files
ottoszikataylorotwell
authored andcommitted
Add orWhere builder methods for day, month and year (#23449)
1 parent 5c32899 commit dd83c6c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Illuminate/Database/Query/Builder.php

+51
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,23 @@ public function whereDay($column, $operator, $value = null, $boolean = 'and')
10471047
return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean);
10481048
}
10491049

1050+
/**
1051+
* Add an "or where day" statement to the query.
1052+
*
1053+
* @param string $column
1054+
* @param string $operator
1055+
* @param mixed $value
1056+
* @return \Illuminate\Database\Query\Builder|static
1057+
*/
1058+
public function orWhereDay($column, $operator, $value = null)
1059+
{
1060+
list($value, $operator) = $this->prepareValueAndOperator(
1061+
$value, $operator, func_num_args() == 2
1062+
);
1063+
1064+
return $this->addDateBasedWhere('Day', $column, $operator, $value, 'or');
1065+
}
1066+
10501067
/**
10511068
* Add a "where month" statement to the query.
10521069
*
@@ -1065,6 +1082,23 @@ public function whereMonth($column, $operator, $value = null, $boolean = 'and')
10651082
return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean);
10661083
}
10671084

1085+
/**
1086+
* Add an "or where month" statement to the query.
1087+
*
1088+
* @param string $column
1089+
* @param string $operator
1090+
* @param mixed $value
1091+
* @return \Illuminate\Database\Query\Builder|static
1092+
*/
1093+
public function orWhereMonth($column, $operator, $value = null)
1094+
{
1095+
list($value, $operator) = $this->prepareValueAndOperator(
1096+
$value, $operator, func_num_args() == 2
1097+
);
1098+
1099+
return $this->addDateBasedWhere('Month', $column, $operator, $value, 'or');
1100+
}
1101+
10681102
/**
10691103
* Add a "where year" statement to the query.
10701104
*
@@ -1083,6 +1117,23 @@ public function whereYear($column, $operator, $value = null, $boolean = 'and')
10831117
return $this->addDateBasedWhere('Year', $column, $operator, $value, $boolean);
10841118
}
10851119

1120+
/**
1121+
* Add an "or where year" statement to the query.
1122+
*
1123+
* @param string $column
1124+
* @param string $operator
1125+
* @param mixed $value
1126+
* @return \Illuminate\Database\Query\Builder|static
1127+
*/
1128+
public function orWhereYear($column, $operator, $value = null)
1129+
{
1130+
list($value, $operator) = $this->prepareValueAndOperator(
1131+
$value, $operator, func_num_args() == 2
1132+
);
1133+
1134+
return $this->addDateBasedWhere('Year', $column, $operator, $value, 'or');
1135+
}
1136+
10861137
/**
10871138
* Add a date based (year, month, day, time) statement to the query.
10881139
*

tests/Database/DatabaseQueryBuilderTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ public function testWhereDayMySql()
308308
$this->assertEquals([0 => 1], $builder->getBindings());
309309
}
310310

311+
public function testOrWhereDayMySql()
312+
{
313+
$builder = $this->getMySqlBuilder();
314+
$builder->select('*')->from('users')->whereDay('created_at', '=', 1)->orWhereDay('created_at', '=', 2);
315+
$this->assertEquals('select * from `users` where day(`created_at`) = ? or day(`created_at`) = ?', $builder->toSql());
316+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
317+
}
318+
311319
public function testWhereMonthMySql()
312320
{
313321
$builder = $this->getMySqlBuilder();
@@ -316,6 +324,14 @@ public function testWhereMonthMySql()
316324
$this->assertEquals([0 => 5], $builder->getBindings());
317325
}
318326

327+
public function testOrWhereMonthMySql()
328+
{
329+
$builder = $this->getMySqlBuilder();
330+
$builder->select('*')->from('users')->whereMonth('created_at', '=', 5)->orWhereMonth('created_at', '=', 6);
331+
$this->assertEquals('select * from `users` where month(`created_at`) = ? or month(`created_at`) = ?', $builder->toSql());
332+
$this->assertEquals([0 => 5, 1 => 6], $builder->getBindings());
333+
}
334+
319335
public function testWhereYearMySql()
320336
{
321337
$builder = $this->getMySqlBuilder();
@@ -324,6 +340,14 @@ public function testWhereYearMySql()
324340
$this->assertEquals([0 => 2014], $builder->getBindings());
325341
}
326342

343+
public function testOrWhereYearMySql()
344+
{
345+
$builder = $this->getMySqlBuilder();
346+
$builder->select('*')->from('users')->whereYear('created_at', '=', 2014)->orWhereYear('created_at', '=', 2015);
347+
$this->assertEquals('select * from `users` where year(`created_at`) = ? or year(`created_at`) = ?', $builder->toSql());
348+
$this->assertEquals([0 => 2014, 1 => 2015], $builder->getBindings());
349+
}
350+
327351
public function testWhereTimeMySql()
328352
{
329353
$builder = $this->getMySqlBuilder();

0 commit comments

Comments
 (0)