From 76c6cac2d45d813e8995a0dbba3b2ed890b9f72a Mon Sep 17 00:00:00 2001 From: Jonathan Reinink Date: Tue, 31 Mar 2020 10:41:07 -0400 Subject: [PATCH 1/2] Add the ability to remove orders from the query builder --- src/Illuminate/Database/Query/Builder.php | 19 ++++++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 29 +++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 4df54acdd578..c2138b3a1eb8 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2017,6 +2017,25 @@ public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') ->limit($perPage); } + /** + * Remove all existing orders and optionally add a new order. + * + * @return static + */ + public function reorder($column = null, $direction = 'asc') + { + $this->orders = null; + $this->unionOrders = null; + $this->bindings['order'] = []; + $this->bindings['unionOrder'] = []; + + if ($column) { + return $this->orderBy($column, $direction); + } + + return $this; + } + /** * Get an array with all orders with a given column removed. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 070cb9b8eaec..33c62b2787a3 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -1108,6 +1108,35 @@ public function testOrderBys() $this->assertEquals([1, 1, 'news', 'opinion'], $builder->getBindings()); } + public function testReorder() + { + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->orderBy('name'); + $this->assertSame('select * from "users" order by "name" asc', $builder->toSql()); + $builder->reorder(); + $this->assertSame('select * from "users"', $builder->toSql()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->orderBy('name'); + $this->assertSame('select * from "users" order by "name" asc', $builder->toSql()); + $builder->reorder('email', 'desc'); + $this->assertSame('select * from "users" order by "email" desc', $builder->toSql()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('first'); + $builder->union($this->getBuilder()->select('*')->from('second')); + $builder->orderBy('name'); + $this->assertSame('(select * from "first") union (select * from "second") order by "name" asc', $builder->toSql()); + $builder->reorder(); + $this->assertSame('(select * from "first") union (select * from "second")', $builder->toSql()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->orderByRaw('?', [true]); + $this->assertEquals([true], $builder->getBindings()); + $builder->reorder(); + $this->assertEquals([], $builder->getBindings()); + } + public function testOrderBySubQueries() { $expected = 'select * from "users" order by (select "created_at" from "logins" where "user_id" = "users"."id" limit 1)'; From 12825073f0ce6286481f34d4883b2cdc69c7a9cc Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 31 Mar 2020 09:55:49 -0500 Subject: [PATCH 2/2] Update Builder.php --- src/Illuminate/Database/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index c2138b3a1eb8..144cb5e84023 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2020,7 +2020,7 @@ public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') /** * Remove all existing orders and optionally add a new order. * - * @return static + * @return $this */ public function reorder($column = null, $direction = 'asc') {