Skip to content

Commit

Permalink
Update DatabaseRule to handle Enums for simple where clause (#47679)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jul 8, 2023
1 parent b83abb4 commit 6a2dff3
Showing 1 changed file with 11 additions and 2 deletions.
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

0 comments on commit 6a2dff3

Please sign in to comment.