Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Add the ability to remove orders from the query builder #32186

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add the ability to remove orders from the query builder
  • Loading branch information
reinink committed Mar 31, 2020
commit 76c6cac2d45d813e8995a0dbba3b2ed890b9f72a
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
Expand Down