Commit fcf5465f authored by Jens Segers's avatar Jens Segers

Adding distinct

parent 7e14145d
......@@ -88,6 +88,15 @@ Examples
$users = User::skip(10)->take(5)->get();
**Distinct**
Distinct requires a field for which to return the distinct values.
$users = User::distinct()->get(array('name'));
// or
$users = User::distinct('name')->get();
**Advanced Wheres**
$users = User::where('name', '=', 'John')->orWhere(function($query)
......
......@@ -122,6 +122,13 @@ class Query extends \Illuminate\Database\Query\Builder {
}
else
{
// Execute distinct
if ($this->distinct)
{
$column = isset($this->columns[0]) ? $this->columns[0] : '_id';
return $this->collection->distinct($column, $wheres);
}
// Get the MongoCursor
$cursor = $this->collection->find($wheres, $this->columns);
......@@ -154,6 +161,23 @@ class Query extends \Illuminate\Database\Query\Builder {
}
}
/**
* Force the query to only return distinct results.
*
* @return \Illuminate\Database\Query\Builder
*/
public function distinct($column = false)
{
$this->distinct = true;
if ($column)
{
$this->columns = array($column);
}
return $this;
}
/**
* Add an "order by" clause to the query.
*
......
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