From 86d9421afc9a7ae1e859829988d11d0d9a37dc0a Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 3 Oct 2024 08:46:16 -0400 Subject: [PATCH] Handle enums in where clause --- src/Query.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 03b856fc..86580fc8 100644 --- a/src/Query.php +++ b/src/Query.php @@ -2,12 +2,14 @@ namespace DirectoryTree\ActiveRedis; +use BackedEnum; use Closure; use DirectoryTree\ActiveRedis\Exceptions\AttributeNotSearchableException; use DirectoryTree\ActiveRedis\Exceptions\ModelNotFoundException; use DirectoryTree\ActiveRedis\Repositories\Repository; use Illuminate\Support\Collection; use Illuminate\Support\Str; +use UnitEnum; class Query { @@ -107,12 +109,26 @@ public function where(array|string $attribute, mixed $value = null): self ); } - $this->wheres[$key] = (string) $value; + $this->wheres[$key] = $this->prepareWhereValue($value); } return $this; } + /** + * Prepare a value for a where clause. + */ + protected function prepareWhereValue(mixed $value): string + { + if ($value instanceof UnitEnum) { + return $value instanceof BackedEnum + ? $value->value + : $value->name; + } + + return (string) $value; + } + /** * Add a where key clause to the query. */