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

[10.x] Update DatabaseRule to handle Enums for simple where clause #47679

Merged
merged 1 commit into from
Jul 8, 2023
Merged
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
13 changes: 11 additions & 2 deletions src/Illuminate/Validation/Rules/DatabaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Validation\Rules;

use BackedEnum;
use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -81,7 +82,7 @@ public function resolveTableName($table)
* Set a "where" constraint on the query.
*
* @param \Closure|string $column
* @param \Illuminate\Contracts\Support\Arrayable|array|string|int|bool|null $value
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|\Closure|array|string|int|bool|null $value
* @return $this
*/
public function where($column, $value = null)
Expand All @@ -98,6 +99,10 @@ public function where($column, $value = null)
return $this->whereNull($column);
}

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

$this->wheres[] = compact('column', 'value');

return $this;
Expand All @@ -107,7 +112,7 @@ public function where($column, $value = null)
* Set a "where not" constraint on the query.
*
* @param string $column
* @param \Illuminate\Contracts\Support\Arrayable|array|string $value
* @param \Illuminate\Contracts\Support\Arrayable|\BackedEnum|array|string $value
* @return $this
*/
public function whereNot($column, $value)
Expand All @@ -116,6 +121,10 @@ public function whereNot($column, $value)
return $this->whereNotIn($column, $value);
}

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

return $this->where($column, '!'.$value);
}

Expand Down