Skip to content

Commit 3a3547e

Browse files
[7.x] Add the ability to remove orders from the query builder (#32186)
* Add the ability to remove orders from the query builder * Update Builder.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 054cf92 commit 3a3547e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Illuminate/Database/Query/Builder.php

+19
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,25 @@ public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
20172017
->limit($perPage);
20182018
}
20192019

2020+
/**
2021+
* Remove all existing orders and optionally add a new order.
2022+
*
2023+
* @return $this
2024+
*/
2025+
public function reorder($column = null, $direction = 'asc')
2026+
{
2027+
$this->orders = null;
2028+
$this->unionOrders = null;
2029+
$this->bindings['order'] = [];
2030+
$this->bindings['unionOrder'] = [];
2031+
2032+
if ($column) {
2033+
return $this->orderBy($column, $direction);
2034+
}
2035+
2036+
return $this;
2037+
}
2038+
20202039
/**
20212040
* Get an array with all orders with a given column removed.
20222041
*

tests/Database/DatabaseQueryBuilderTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,35 @@ public function testOrderBys()
11081108
$this->assertEquals([1, 1, 'news', 'opinion'], $builder->getBindings());
11091109
}
11101110

1111+
public function testReorder()
1112+
{
1113+
$builder = $this->getBuilder();
1114+
$builder->select('*')->from('users')->orderBy('name');
1115+
$this->assertSame('select * from "users" order by "name" asc', $builder->toSql());
1116+
$builder->reorder();
1117+
$this->assertSame('select * from "users"', $builder->toSql());
1118+
1119+
$builder = $this->getBuilder();
1120+
$builder->select('*')->from('users')->orderBy('name');
1121+
$this->assertSame('select * from "users" order by "name" asc', $builder->toSql());
1122+
$builder->reorder('email', 'desc');
1123+
$this->assertSame('select * from "users" order by "email" desc', $builder->toSql());
1124+
1125+
$builder = $this->getBuilder();
1126+
$builder->select('*')->from('first');
1127+
$builder->union($this->getBuilder()->select('*')->from('second'));
1128+
$builder->orderBy('name');
1129+
$this->assertSame('(select * from "first") union (select * from "second") order by "name" asc', $builder->toSql());
1130+
$builder->reorder();
1131+
$this->assertSame('(select * from "first") union (select * from "second")', $builder->toSql());
1132+
1133+
$builder = $this->getBuilder();
1134+
$builder->select('*')->from('users')->orderByRaw('?', [true]);
1135+
$this->assertEquals([true], $builder->getBindings());
1136+
$builder->reorder();
1137+
$this->assertEquals([], $builder->getBindings());
1138+
}
1139+
11111140
public function testOrderBySubQueries()
11121141
{
11131142
$expected = 'select * from "users" order by (select "created_at" from "logins" where "user_id" = "users"."id" limit 1)';

0 commit comments

Comments
 (0)