Skip to content

Commit

Permalink
Handle enums in where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Oct 3, 2024
1 parent 3ccf277 commit 86d9421
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 86d9421

Please sign in to comment.