Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Add orWhere builder methods for day, month and year #23449

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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