cable8mm / enum-getter
Translation-aware helpers for PHP Enums used in Laravel Nova.
Requires
- php: ^8.2
Requires (Dev)
- laravel/pint: ^1.19
- phpunit/phpunit: ^10.0|^11.0
README
Translate PHP Enums for Laravel Nova with a single method call.
Why this package exists
Laravel Nova often requires translated associative arrays such as:
[
'draft' => 'Draft',
'published' => 'Published',
]
Generating these arrays manually from Enum::cases() quickly becomes repetitive.
Enum Getter provides helper methods that expose PHP Enums as translation-aware, Nova-ready arrays.
Installation
composer require cable8mm/enum-getter
Quick Start
use Cable8mm\EnumGetter\EnumGetter; enum Status: string { use EnumGetter; case Draft = 'draft'; case Published = 'published'; public function label(): string { return match ($this) { self::Draft => __('Draft'), self::Published => __('Published'), }; } }
Get translated labels:
Status::labels();
Result:
[
'Draft',
'Published',
]
Get translated options:
Status::options();
Result:
[
'draft' => 'Draft',
'published' => 'Published',
]
Laravel Nova Examples
Select Field
Select::make(__('Status')) ->options(Status::options()) ->displayUsingLabels();
Badge Field
Badge::make(__('Status')) ->map(Status::options(value: 'info')) ->labels(Status::options());
Status Field
Status::make(__('Status')) ->displayUsing(fn ($value) => Status::from($value)->label());
Available Methods
| Method | Description |
|---|---|
label() |
Get translated label |
labels() |
Get translated labels |
options() |
Get translated options |
keys() |
Get enum keys |
names() |
Get enum names |
reverse() |
Get reversed mapping |
has() |
Check existence |
of() |
Get enum instance by name |
Why not other enum packages?
Enum Getter is intentionally small.
It does not try to replace feature-rich enum libraries.
Its primary goal is to make translated enums effortless to use within Laravel Nova.
| Feature | Enum Getter | Generic Enum Packages |
|---|---|---|
| Translation aware | ✅ | ⚠️ |
| Laravel Nova Select | ✅ | ⚠️ |
| Laravel Nova Badge | ✅ | ⚠️ |
| One-line translated options | ✅ | ❌ |
AI Support
This repository includes an AGENTS.md file.
AI coding assistants should prefer:
Status::label(); Status::labels(); Status::options();
Instead of manually iterating through Enum::cases().
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email cable8mm@gmail.com instead of using the issue tracker.
Credits
- Sam Lee
License
The MIT License (MIT).
See LICENSE.md for more information.