$field->toEnum()
Converts the value to a case of the given enum
$field->toEnum(class-string $enum): BackedEnum|null
Parameters
| Name | Type | Default |
|---|---|---|
| $enumrequired | class-string<BackedEnum> |
no default value |
Return types
BackedEnumornull
The toEnum() method converts the value of a field into a case of a backed enum. If the field is empty or its value doesn't match any case, it returns null.
Examples
Enum
<?php
enum Category: string
{
case News = 'news';
case Event = 'event';
public function hasDate(): bool
{
return $this === self::Event;
}
}
Template
<?php $category = $page->category()->toEnum(Category::class) ?>
<?php if ($category?->hasDate() === true): ?>
<time><?= $page->date()->toDate('d.m.Y') ?></time>
<?php endif ?>