Skip to content
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
55 changes: 55 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,24 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa
return $this;
}

/**
* Add a where between statement using columns to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereBetweenColumns($column, array $values, $boolean = 'and', $not = false)
{
$type = 'betweenColumns';

$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');

return $this;
}

/**
* Add an or where between statement to the query.
*
Expand All @@ -1095,6 +1113,18 @@ public function orWhereBetween($column, array $values)
return $this->whereBetween($column, $values, 'or');
}

/**
* Add an or where between statement using columns to the query.
*
* @param string $column
* @param array $values
* @return $this
*/
public function orWhereBetweenColumns($column, array $values)
{
return $this->whereBetweenColumns($column, $values, 'or');
}

/**
* Add a where not between statement to the query.
*
Expand All @@ -1108,6 +1138,19 @@ public function whereNotBetween($column, array $values, $boolean = 'and')
return $this->whereBetween($column, $values, $boolean, true);
}

/**
* Add a where not between statement using columns to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @return $this
*/
public function whereNotBetweenColumns($column, array $values, $boolean = 'and')
{
return $this->whereBetweenColumns($column, $values, $boolean, true);
}

/**
* Add an or where not between statement to the query.
*
Expand All @@ -1120,6 +1163,18 @@ public function orWhereNotBetween($column, array $values)
return $this->whereNotBetween($column, $values, 'or');
}

/**
* Add an or where not between statement using columns to the query.
*
* @param string $column
* @param array $values
* @return $this
*/
public function orWhereNotBetweenColumns($column, array $values)
{
return $this->whereNotBetweenColumns($column, $values, 'or');
}

/**
* Add an "or where not null" clause to the query.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ protected function whereBetween(Builder $query, $where)
return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}

/**
* Compile a "between" where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBetweenColumns(Builder $query, $where)
{
$between = $where['not'] ? 'not between' : 'between';

$min = $this->wrap(reset($where['values']));

$max = $this->wrap(end($where['values']));

return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}

/**
* Compile a "where date" clause.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,24 @@ public function testWhereBetweens()
$this->assertEquals([], $builder->getBindings());
}

public function testWhereBetweenColumns()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereBetweenColumns('id', ['users.created_at', 'users.updated_at']);
$this->assertSame('select * from "users" where "id" between "users"."created_at" and "users"."updated_at"', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotBetweenColumns('id', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" not between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereBetweenColumns('id', [new Raw(1), new Raw(2)]);
$this->assertSame('select * from "users" where "id" between 1 and 2', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testBasicOrWheres()
{
$builder = $this->getBuilder();
Expand Down