Skip to content

Commit 489daae

Browse files
authored
chore(database): support order by raw with a shorthand (#1533)
1 parent accab81 commit 489daae

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ public function chunk(Closure $closure, int $amountPerChunk = 200): void
168168
*/
169169
public function orderBy(string $field, Direction $direction = Direction::ASC): self
170170
{
171+
if (str_contains($field, ' ')) {
172+
return $this->orderByRaw($field);
173+
}
174+
171175
$this->select->orderBy[] = new OrderByStatement("`{$field}` {$direction->value}");
172176

173177
return $this;

tests/Integration/Database/Builder/SelectQueryBuilderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ public function test_order_by_with_field_defaults_to_asc(): void
195195
$this->assertSame('A', $book->title);
196196
}
197197

198+
public function test_order_by_raw_shorthand(): void
199+
{
200+
$this->migrate(
201+
CreateMigrationsTable::class,
202+
CreatePublishersTable::class,
203+
CreateAuthorTable::class,
204+
CreateBookTable::class,
205+
);
206+
207+
Book::new(title: 'A')->save();
208+
Book::new(title: 'B')->save();
209+
Book::new(title: 'C')->save();
210+
Book::new(title: 'D')->save();
211+
212+
$book = Book::select()->orderBy('title DESC')->first();
213+
$this->assertSame('D', $book->title);
214+
}
215+
198216
public function test_order_by_sql_generation(): void
199217
{
200218
$this->assertSameWithoutBackticks(

0 commit comments

Comments
 (0)