Closed nikrovir closed 1 year ago
If you solved this, please share.
I solve my problem in this way:
use BenSampo\Enum\Enum;
use BenSampo\Enum\Exceptions\InvalidEnumMemberException;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Support\DataProperty;
class EnumCaster implements Cast
{
/**
* @throws InvalidEnumMemberException
*/
public function cast(DataProperty $property, mixed $value, array $context): Enum
{
$type = $this->type ?? $property->type->findAcceptedTypeForBaseType(Enum::class);
if ($value instanceof $type) {
return $value;
}
return $type::fromValue($value);
}
}
Hello! Anyone have idea how to cast string into enum using Spatie Laravel-data package?