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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/string backed enum in order by #54042

Closed
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
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database;

use BackedEnum;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function wrapTable($table)
/**
* Wrap a value in keyword identifiers.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $value
* @param \Illuminate\Contracts\Database\Query\Expression|\BackedEnum|string $value
* @return string
*/
public function wrap($value)
Expand All @@ -81,6 +82,10 @@ public function wrap($value)
return $this->getValue($value);
}

if ($value instanceof BackedEnum) {
$value = $value->value;
}
Comment on lines +85 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have used Illuminate\Support\enum_value():

Suggested change
if ($value instanceof BackedEnum) {
$value = $value->value;
}
$value = enum_value($value);


// If the value being wrapped has a column alias we will need to separate out
// the pieces so we can wrap each of the segments of the expression on its
// own, and then join these both back together using the "as" connector.
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,7 @@ public function orHavingRaw($sql, array $bindings = [])
/**
* Add an "order by" clause to the query.
*
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<*>|\Illuminate\Contracts\Database\Query\Expression|string $column
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder<*>|\Illuminate\Contracts\Database\Query\Expression|string|\BackedEnum $column
* @param string $direction
* @return $this
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,17 @@ public function testOrderByInvalidDirectionParam()
$builder->select('*')->from('users')->orderBy('age', 'asec');
}

public function testOrderByBackedEnums()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->orderBy(StringStatus::done)->orderBy('age', 'desc');

$builder = $this->getBuilder();
$builder->select('*')->from('users')->orderBy(IntegerStatus::done)->orderBy('age', 'desc');

$this->assertSame('select * from "users" order by "2" asc, "age" desc', $builder->toSql());
}

public function testHavings()
{
$builder = $this->getBuilder();
Expand Down