Commit cbb5273d authored by Jens Segers's avatar Jens Segers

Merge pull request #149 from khamkham/master

Add natural reverse (descending) sort option
parents 93b7afd5 9a5b24f1
...@@ -281,17 +281,17 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -281,17 +281,17 @@ class Builder extends \Illuminate\Database\Query\Builder {
* @param string $direction * @param string $direction
* @return Builder * @return Builder
*/ */
public function orderBy($column, $direction = null) public function orderBy($column, $direction = 'asc')
{ {
if (is_null($direction) && $column == 'natural') $direction = (strtolower($direction) == 'asc' ? 1 : -1);
if ($column == 'natural')
{ {
$this->orders['$natural'] = 1; $this->orders['$natural'] = $direction;
} }
else else
{ {
$direction = $direction ?: 'asc'; $this->orders[$column] = $direction;
$this->orders[$column] = (strtolower($direction) == 'asc' ? 1 : -1);
} }
return $this; return $this;
......
...@@ -148,8 +148,14 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -148,8 +148,14 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$user = User::whereNotNull('age')->orderBy('age', 'desc')->first(); $user = User::whereNotNull('age')->orderBy('age', 'desc')->first();
$this->assertEquals(37, $user->age); $this->assertEquals(37, $user->age);
$user = User::whereNotNull('age')->orderBy('natural')->first(); $user = User::whereNotNull('age')->orderBy('natural', 'asc')->first();
$this->assertEquals(35, $user->age); $this->assertEquals(35, $user->age);
$user = User::whereNotNull('age')->orderBy('natural', 'ASC')->first();
$this->assertEquals(35, $user->age);
$user = User::whereNotNull('age')->orderBy('natural', 'desc')->first();
$this->assertEquals(35, $user->age);
} }
public function testGroupBy() public function testGroupBy()
......
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