Skip to content

Commit

Permalink
Add orWhere builder methods for day, month and year (#23449)
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoszika authored and taylorotwell committed Mar 9, 2018
1 parent 5c32899 commit dd83c6c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,23 @@ public function whereDay($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean);
}

/**
* Add an "or where day" statement to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @return \Illuminate\Database\Query\Builder|static
*/
public function orWhereDay($column, $operator, $value = null)
{
list($value, $operator) = $this->prepareValueAndOperator(
$value, $operator, func_num_args() == 2
);

return $this->addDateBasedWhere('Day', $column, $operator, $value, 'or');
}

/**
* Add a "where month" statement to the query.
*
Expand All @@ -1065,6 +1082,23 @@ public function whereMonth($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean);
}

/**
* Add an "or where month" statement to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @return \Illuminate\Database\Query\Builder|static
*/
public function orWhereMonth($column, $operator, $value = null)
{
list($value, $operator) = $this->prepareValueAndOperator(
$value, $operator, func_num_args() == 2
);

return $this->addDateBasedWhere('Month', $column, $operator, $value, 'or');
}

/**
* Add a "where year" statement to the query.
*
Expand All @@ -1083,6 +1117,23 @@ public function whereYear($column, $operator, $value = null, $boolean = 'and')
return $this->addDateBasedWhere('Year', $column, $operator, $value, $boolean);
}

/**
* Add an "or where year" statement to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @return \Illuminate\Database\Query\Builder|static
*/
public function orWhereYear($column, $operator, $value = null)
{
list($value, $operator) = $this->prepareValueAndOperator(
$value, $operator, func_num_args() == 2
);

return $this->addDateBasedWhere('Year', $column, $operator, $value, 'or');
}

/**
* Add a date based (year, month, day, time) statement to the query.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ public function testWhereDayMySql()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testOrWhereDayMySql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereDay('created_at', '=', 1)->orWhereDay('created_at', '=', 2);
$this->assertEquals('select * from `users` where day(`created_at`) = ? or day(`created_at`) = ?', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testWhereMonthMySql()
{
$builder = $this->getMySqlBuilder();
Expand All @@ -316,6 +324,14 @@ public function testWhereMonthMySql()
$this->assertEquals([0 => 5], $builder->getBindings());
}

public function testOrWhereMonthMySql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereMonth('created_at', '=', 5)->orWhereMonth('created_at', '=', 6);
$this->assertEquals('select * from `users` where month(`created_at`) = ? or month(`created_at`) = ?', $builder->toSql());
$this->assertEquals([0 => 5, 1 => 6], $builder->getBindings());
}

public function testWhereYearMySql()
{
$builder = $this->getMySqlBuilder();
Expand All @@ -324,6 +340,14 @@ public function testWhereYearMySql()
$this->assertEquals([0 => 2014], $builder->getBindings());
}

public function testOrWhereYearMySql()
{
$builder = $this->getMySqlBuilder();
$builder->select('*')->from('users')->whereYear('created_at', '=', 2014)->orWhereYear('created_at', '=', 2015);
$this->assertEquals('select * from `users` where year(`created_at`) = ? or year(`created_at`) = ?', $builder->toSql());
$this->assertEquals([0 => 2014, 1 => 2015], $builder->getBindings());
}

public function testWhereTimeMySql()
{
$builder = $this->getMySqlBuilder();
Expand Down

0 comments on commit dd83c6c

Please sign in to comment.