Commit a90d8bdc authored by Jens Segers's avatar Jens Segers

Adding whereBetween

parent 97fd865d
...@@ -76,6 +76,10 @@ Examples ...@@ -76,6 +76,10 @@ Examples
$users = User::whereIn('age', array(16, 18, 20))->get(); $users = User::whereIn('age', array(16, 18, 20))->get();
**Using Where Between**
$users = User::whereBetween('votes', array(1, 100))->get();
**Where null** **Where null**
$users = User::whereNull('updated_at')->get(); $users = User::whereNull('updated_at')->get();
......
...@@ -210,6 +210,25 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -210,6 +210,25 @@ class Query extends \Illuminate\Database\Query\Builder {
return $this; return $this;
} }
/**
* Add a where between statement to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @return \Illuminate\Database\Query\Builder
*/
public function whereBetween($column, array $values, $boolean = 'and')
{
$type = 'between';
$this->wheres[] = compact('column', 'type', 'boolean', 'values');
$this->bindings = array_merge($this->bindings, $values);
return $this;
}
/** /**
* Insert a new record into the database. * Insert a new record into the database.
* *
...@@ -312,7 +331,7 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -312,7 +331,7 @@ class Query extends \Illuminate\Database\Query\Builder {
public function compileWheres() public function compileWheres()
{ {
if (!$this->wheres) return array(); if (!$this->wheres) return array();
$wheres = array(); $wheres = array();
foreach ($this->wheres as $i=>&$where) foreach ($this->wheres as $i=>&$where)
...@@ -334,6 +353,12 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -334,6 +353,12 @@ class Query extends \Illuminate\Database\Query\Builder {
$method = "compileWhere{$where['type']}"; $method = "compileWhere{$where['type']}";
$compiled = $this->{$method}($where); $compiled = $this->{$method}($where);
// Check for or
if ($where['boolean'] == 'or')
{
$compiled = array('$or' => array($compiled));
}
// Merge compiled where // Merge compiled where
$wheres = array_merge_recursive($wheres, $compiled); $wheres = array_merge_recursive($wheres, $compiled);
} }
...@@ -367,11 +392,6 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -367,11 +392,6 @@ class Query extends \Illuminate\Database\Query\Builder {
$query = array($column => array($this->conversion[$operator] => $value)); $query = array($column => array($this->conversion[$operator] => $value));
} }
if ($boolean == 'or')
{
return array('$or' => array($query));
}
return $query; return $query;
} }
...@@ -379,15 +399,7 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -379,15 +399,7 @@ class Query extends \Illuminate\Database\Query\Builder {
{ {
extract($where); extract($where);
// Compile subquery return $query->compileWheres();
$compiled = $query->compileWheres();
if ($boolean == 'or')
{
return array('$or' => array($compiled));
}
return $compiled;
} }
public function compileWhereIn($where) public function compileWhereIn($where)
...@@ -413,6 +425,17 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -413,6 +425,17 @@ class Query extends \Illuminate\Database\Query\Builder {
return $this->compileWhereBasic($where); return $this->compileWhereBasic($where);
} }
public function compileWherebetween($where)
{
extract($where);
return array(
$column => array(
'$gte' => $values[0],
'$lte' => $values[1])
);
}
/** /**
* Get a new instance of the query builder. * Get a new instance of the query builder.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment