Livewire 3 hydrates backed enums on its own, so an enum that uses BaseEnum can be a typed public property without any setup:
namespace App\Livewire;
use App\Enums\UserRole;
use Livewire\Component;
class UserForm extends Component
{
public UserRole $role = UserRole::CUSTOMER;
}asSelectArray() provides the options for a <select>, with translated labels if the enum is localized:
<select wire:model="role">
@foreach (\App\Enums\UserRole::asSelectArray() as $value => $label)
<option value="{{ $value }}">{{ $label }}</option>
@endforeach
</select>The trait also has two conversion helpers:
| Call | Returns |
|---|---|
UserRole::ADMIN->toLivewire() |
['value' => 10, 'label' => 'ADMIN'] |
UserRole::fromLivewire(['value' => 50, 'label' => 'CUSTOMER']) |
UserRole::CUSTOMER |
UserRole::fromLivewire((object) ['value' => 50]) |
UserRole::CUSTOMER |
UserRole::fromLivewire(UserRole::ADMIN) |
UserRole::ADMIN |
label is the case name, not the translated label.
fromLivewire() throws InvalidArgumentException for anything else: a raw value such as 50, or an array or object whose value is missing or is not an int or string.
Livewire calls these methods only on classes that implement Livewire\Wireable. An enum does not need that interface, because Livewire already handles backed enums as described above. The helpers are there for code that wants the value and label shape.
- Helper methods:
asSelectArray()and the other list helpers - Serving enums to JavaScript: enums for Vue, React or Alpine