Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ public function chunk(Closure $closure, int $amountPerChunk = 200): void
*/
public function orderBy(string $field, Direction $direction = Direction::ASC): self
{
if (str_contains($field, ' ')) {
return $this->orderByRaw($field);
}

$this->select->orderBy[] = new OrderByStatement("`{$field}` {$direction->value}");

return $this;
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Database/Builder/SelectQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ public function test_order_by_with_field_defaults_to_asc(): void
$this->assertSame('A', $book->title);
}

public function test_order_by_raw_shorthand(): void
{
$this->migrate(
CreateMigrationsTable::class,
CreatePublishersTable::class,
CreateAuthorTable::class,
CreateBookTable::class,
);

Book::new(title: 'A')->save();
Book::new(title: 'B')->save();
Book::new(title: 'C')->save();
Book::new(title: 'D')->save();

$book = Book::select()->orderBy('title DESC')->first();
$this->assertSame('D', $book->title);
}

public function test_order_by_sql_generation(): void
{
$this->assertSameWithoutBackticks(
Expand Down
Loading