PHPackages                             datomatic/laravel-enum-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. datomatic/laravel-enum-helper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

datomatic/laravel-enum-helper
=============================

Simple opinionated framework agnostic PHP 8.1 enum helper for Laravel

v2.1.4(2mo ago)33219.6k↓12%43MITPHPPHP ^8.1CI passing

Since Jun 18Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/datomatic/laravel-enum-helper)[ Packagist](https://packagist.org/packages/datomatic/laravel-enum-helper)[ RSS](/packages/datomatic-laravel-enum-helper/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (26)Versions (23)Used By (3)

[![Enum Helper-Dark](branding/laravel-dark.png#gh-dark-mode-only)](branding/laravel-dark.png#gh-dark-mode-only)[![Enum Helper-Light](branding/laravel-light.png#gh-light-mode-only)](branding/laravel-light.png#gh-light-mode-only)

Laravel Enum Helper
===================

[](#laravel-enum-helper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2ff2c8f03a9e03e9ea08d7065904041ecedd02af9dbc20c1226237edca164bf7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461746f6d617469632f6c61726176656c2d656e756d2d68656c7065722e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/datomatic/laravel-enum-helper)[![Pest Tests number](https://camo.githubusercontent.com/a84a8eb5fc49d339036a28f37ed15d1af094191a6556507108b90a9581e3972a/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d2532337465737473266d6573736167653d393926636f6c6f723d464638384641267374796c653d666f722d7468652d6261646765266c6f676f3d646174613a696d6167652f706e673b6261736536342c6956424f5277304b47676f414141414e5355684555674141414277414141416343414d414141424630792b6d414141426956424d564555414141442f69507639795033586d2b6a2f6d502f2f7766566a36374a653662502f682f70567836703631373564353757517963662b69506e2f695072736e657a417264332b742f7170764e4a64364c502f6a5070753672762f6c50722f6b5070633537542f7276746335374e7036726a336f506c3337634c2f74666e2f77763964366272582f2f4c2f672f72596e2b6e2f677672576d2b6469364c582b6a5072736b6647577a4d707436626c6e3462646435374a6b364c575379636a2b76507175774e566f36726465366250376e7676596e75703931622f2b7666762f6c76746335374f71764e544673392f2f742f746435374c39742f722f69507064364c506170656a2f6f76703236627879363776396c666c64364c4a72344c6a777376622f7876332f6a7633397a76317436627547356354447265483569766c6335724a7936373656346378623537442f792f6835304d4f79344f4355786356613737582f69507065364c502f6a502b7075394c38742f2f2f7476755179636641724e7870364c7a41726431353172372f692f396e3462622f6a2f39653672542f69667237696672736b764c596e7568693837746738626c673762662f76762f2f6c502b77784e746a3962332f71762f2f6f502f2b69767a2f6c2f7238696672796e2f66766c505466705044656f66444b74756a48744f5758314e462f3473654333635238327346753763426f354c694d77504d724141414157485253546c4d412f577638464149432f644d452f576a2b337445472f507637393847316f48526a53306b314c4273574467734a2f7633362b66547938657a6e342b4c683239584e7a4d7a4c79734c41774c53777236366f704a71616b592b4e69344a37656e643062476c7059313158553034384b69636d49523866697a6c2b767741414156644a524546554b4d39747a325658416c4551674f4642425552706b453637753775374531594659516c31536276726c7a7444694c7673732b66632f6643656d584d76414568684b71553735395031724c6f7855445579456839665048307a37414c69567245592b53534e74784e533275706f75597637684f4c313931614b56735a48555467626e51505167446b7134637448646f516d54576d573457467a6c56554456704e4b58663266577057625a497755712f68636d6a5747596e536131705a5a6a456f6f6d72456456416973443743583647456234307271544f4478436a32314f6a444f766a5256386c326a68756442446368672f46556244495a4349547a775179483661392b32414d44626d3947666c74466e786741647451575567514a6c345651713337755063536e7366595a7a61763645773138665134665559504d37516e34755369496479783573614a36542b2f332b354b536c7473686963774932557066414b452f616f415254766e6e374b4d594d646c41557957525379484e324a65553432486c43693454737a5448636d756a33694d566450354a7a6f7941574e7a693654335a47724d46436c694b334241715253432f42322b3649787659594e634f2b324e7066762b79466931304c66425541414141415355564f524b35435949493d)](https://github.com/datomatic/laravel-enum-helper/tree/main/tests)[![GitHub Tests Action Status](https://camo.githubusercontent.com/63d93f1df4adc47b6c620ffabed908431a6af2794817efced2eeb75f850510ee/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6461746f6d617469632f6c61726176656c2d656e756d2d68656c7065722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d746573747326636f6c6f723d354645384233267374796c653d666f722d7468652d6261646765266c6f676f3d646174613a696d6167652f706e673b6261736536342c6956424f5277304b47676f414141414e5355684555674141414277414141416343414d414141424630792b6d414141426956424d564555414141442f69507639795033586d2b6a2f6d502f2f7766566a36374a653662502f682f70567836703631373564353757517963662b69506e2f695072736e657a417264332b742f7170764e4a64364c502f6a5070753672762f6c50722f6b5070633537542f7276746335374e7036726a336f506c3337634c2f74666e2f77763964366272582f2f4c2f672f72596e2b6e2f677672576d2b6469364c582b6a5072736b6647577a4d707436626c6e3462646435374a6b364c575379636a2b76507175774e566f36726465366250376e7676596e75703931622f2b7666762f6c76746335374f71764e544673392f2f742f746435374c39742f722f69507064364c506170656a2f6f76703236627879363776396c666c64364c4a72344c6a777376622f7876332f6a7633397a76317436627547356354447265483569766c6335724a7936373656346378623537442f792f6835304d4f79344f4355786356613737582f69507065364c502f6a502b7075394c38742f2f2f7476755179636641724e7870364c7a41726431353172372f692f396e3462622f6a2f39653672542f69667237696672736b764c596e7568693837746738626c673762662f76762f2f6c502b77784e746a3962332f71762f2f6f502f2b69767a2f6c2f7238696672796e2f66766c505466705044656f66444b74756a48744f5758314e462f3473654333635238327346753763426f354c694d77504d724141414157485253546c4d412f577638464149432f644d452f576a2b337445472f507637393847316f48526a53306b314c4273574467734a2f7633362b66547938657a6e342b4c683239584e7a4d7a4c79734c41774c53777236366f704a71616b592b4e69344a37656e643062476c7059313158553034384b69636d49523866697a6c2b767741414156644a524546554b4d39747a325658416c4551674f4642425552706b453637753775374531594659516c31536276726c7a7444694c7673732b66632f6643656d584d76414568684b71553735395031724c6f7855445579456839665048307a37414c69567245592b53534e74784e533275706f75597637684f4c313931614b56735a48555467626e51505167446b7134637448646f516d54576d573457467a6c56554456704e4b58663266577057625a497755712f68636d6a5747596e536131705a5a6a456f6f6d72456456416973443743583647456234307271544f4478436a32314f6a444f766a5256386c326a68756442446368672f46556244495a4349547a775179483661392b32414d44626d3947666c74466e786741647451575567514a6c345651713337755063536e7366595a7a61763645773138665134665559504d37516e34755369496479783573614a36542b2f332b354b536c7473686963774932557066414b452f616f415254766e6e374b4d594d646c41557957525379484e324a65553432486c43693454737a5448636d756a33694d566450354a7a6f7941574e7a693654335a47724d46436c694b334241715253432f42322b3649787659594e634f2b324e7066762b79466931304c66425541414141415355564f524b35435949493d)](https://github.com/datomatic/laravel-enum-helper/actions/workflows/run-tests.yml)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/0f384b1fe279dc8a320a5f1a8418ff75f5bbf0c88b224b14f66d2737cd31b2e1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6461746f6d617469632f6c61726176656c2d656e756d2d68656c7065722f70696e742e796d6c3f6c6162656c3d636f64652532307374796c6526636f6c6f723d354645384233267374796c653d666f722d7468652d6261646765)](https://github.com/datomatic/laravel-enum-helper/actions/workflows/php-cs-fixer.yml)[![Total Downloads](https://camo.githubusercontent.com/9924aa63b8c1093759f5262e10626efa70fe2efd2f1016ffb38b886748f61437/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6461746f6d617469632f6c61726176656c2d656e756d2d68656c7065722e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/datomatic/laravel-enum-helper)

This is an extension of the [datomatic/enum-helper](https://github.com/datomatic/enum-helper) package based on Laravel Framework. The package consists on a `LaravelEnumHelper` trait that extends `EnumInvokable`, `EnumFroms`, `EnumNames`, `EnumValues`, `EnumEquality`, `EnumDescription` and add dynamic methods to return a translation or "property" method and relative helper methods.

You can think about it as an [`EnumDescription` trait](https://github.com/datomatic/enum-helper#descriptions-and-translations) but completely dynamic.

So you can define a custom `method()` and have these functions available: `[method]s()`, `[method]sByName()`, `[method]sByValue()`. For example with `color()` you obtain `colors()`, `colorsByName()`, `colorsByValue()` methods.

The cool thing is you can also avoid writing the method and write only the translations. For example, you can define the property `excerpt` by writing only the translations on enums.php [(see below for explanation)](#translations)and obtain `excerpt()`, `excerpts()`, `excerptsByName()`, `excerptsByValue()` methods.

The package use the [Laravel `Pluralizer` component](https://laravel.com/docs/localization#pluralization-language) to get the singular method to call or to translate.

Related Packages
----------------

[](#related-packages)

- [datomatic/laravel-enum-collections](https://github.com/datomatic/laravel-enum-collections) if you need to save and interact many enum values on one field.
- [datomatic/nova-enum-field](https://github.com/datomatic/nova-enum-field) if you are using [Laravel Nova](https://nova.laravel.com/), this package adds the PHP 8.1 `enum`, `EnumDescription` and `LaravelEnumHelper` traits support, with a Nova Select and Nova Boolean filters ready out of the box.

The `enum-helper` base package summary
--------------------------------------

[](#the-enum-helper-base-package-summary)

You should see the `datomatic/enum-helper` details on his [repository](https://github.com/datomatic/enum-helper), this is a summary:

- **Invokable cases**: get the value of enum "invoking" it statically
- **Construct enum by name or value**: `from()`, `tryFrom()`, `fromName()`, `tryFromName()` for all enums
- **Enums Inspection**: `isPure()`, `isBacked()`, `has()`, `hasName()`, `hasValue()` methods
- **Enums Equality**: `is()`, `isNot()`, `in()`, `notIn()` methods
- **Names**: methods to have a list of case names (`names()`, `namesByValue()`)
- **Values**: methods to have a list of case values (`values()`, `valuesByName()`)
- **Unique ID**: get an unique identifier from instance or instance from identifier (`uniqueId()`, `fromUniqueId()`)
- **Descriptions &amp; Translations**: add description to enum with optional translation (`description()`,`descriptions()`,`descriptionsByName()`,`descriptionsByValue()`)

Installation
------------

[](#installation)

Laravel 8 or above and PHP 8.1+ are required.

```
composer require datomatic/laravel-enum-helper
```

Usage
-----

[](#usage)

You can use this functionality simply using the `LaravelEnumHelper` trait.

```
use Datomatic\LaravelEnumHelper\LaravelEnumHelper;

// Pure enum
enum Status
{
    use LaravelEnumHelper;

    case PENDING;
    case ACCEPTED;
    case DISCARDED;
    case NO_RESPONSE;

    public function color(): string
    {
        return match ($this) {
            self::PENDING => '#000000',
            self::ACCEPTED => '#0000FF',
            self::DISCARDED => '#FF0000',
            self::NO_RESPONSE => '#FFFFFF',
        };
    }
}

// BackedEnum
enum StatusString
{
    use LaravelEnumHelper;

    case PENDING = 'P';
    case ACCEPTED = 'A';
    case DISCARDED = 'D';
    case NO_RESPONSE = 'N';

    // or public function color(?string $lang = null): string
    public function color(): string
    {
        ...
    }
```

After that you can define a custom "property" method like `color()` or `color(?string $lang = null)` or define the translations instead.

Annotation command
------------------

[](#annotation-command)

A convenient command is available to generate DocBlock annotations for enum classes.

```
php artisan enum:annotate {class?} {--folder=}

# Single enum
php artisan enum:annotate "Enums\FQN\ClassName"
# All enums in a folder
php artisan enum:annotate --folder=Enums/Folder/Path
```

This command generate the phpDoc annotations for [invokable case methods](https://github.com/datomatic/enum-helper?tab=readme-ov-file#invokable-cases).

Translations
------------

[](#translations)

Using this trait there is 2 way to manage translation strings.

### Using Short Keys

[](#using-short-keys)

Language strings may be stored in `enums.php` files within the `lang` directory. Within this directory, there may be subdirectories for each language supported by the application.

```
/lang
    /en
        enums.php
    /it
        enums.php

```

All language files return an array of keyed strings. The array has 3 levels:

- first level key: the complete class name of the enum
- second level key: the name of the property to translate
- third level key: the name/value of the enum case.

The default property is `description` so if you use only that property you can use just a 2 level array.

```
// /lang/it/enums.php

return [
    // If you need to translate just the description property
    Status::class => [
        Status::PENDING() => 'In attesa', // using invokable trait functionality
        'ACCEPTED' => 'Accettato', // using the name of pure enum case
        'DISCARDED' => 'Rifiutato',
        'NO_RESPONSE' => 'Nessuna Risposta',
    ],
     // If you need to translate multiple properties (e.g. description, excerpt)
    StatusString::class => [
        'description' => [ // using invokable trait functionality
            StatusString::PENDING() => 'In attesa',
            StatusString::ACCEPTED() => 'Accettato',
            ...
        ],
        'excerpt' => [ // using the value of BackedEnum case
            "P" => 'In attesa',
            "A" => 'Accettato',
    ...
```

### Using Translation Strings As Keys

[](#using-translation-strings-as-keys)

Language strings are stored as JSON files in the lang directory (e.g. `lang/it.json`). In this example the `description` property is translated:

```
{
  "enums.Namespace\\StatusString.description.P": "In attesa",
  "...":"..."
}
```

But if you want to use this way, you can simply define the method on Enum class like this `description` method.

```
public function description(?string $lang = null): string
{
    return match ($this) {
        self::PENDING => __('Await decision', $lang),
        self::ACCEPTED => __('Recognized valid', $lang),
        self::DISCARDED => __('No longer useful', $lang),
        self::NO_RESPONSE => __('No response', $lang),
    };
```

`[property]()` method
---------------------

[](#property-method)

This dynamic method try to resolve `property()` method on the enum.
If the method not exist try to translate the instance value with the framework translation function `__("enums.Namespace\EnumClass.property.CASE_NAME")`.
Because the default property is `description`, if there isn't a translation to that property there is a fallback that takes the case name humanized. With the other properties, if the translation does not exist, the package throws a `TranslationMissing` exception.

static `[property]s()` method
-----------------------------

[](#static-propertys-method)

This dynamic method gets a list of case `property()` returns of the enum. The name of the method is the plural of the property so if you are using `property` it will be `properties()`.

```
StatusString::descriptions(); // ['Await decision','Recognized valid','No longer useful','No response']
StatusString::colors(); // ['#000000','#0000FF','#FF0000','#FFFFFF']
// Subset
StatusString::descriptions([StatusString::ACCEPTED, StatusString::NO_RESPONSE]); // ['Recognized valid','No response']
StatusString::colors([StatusString::ACCEPTED, StatusString::NO_RESPONSE]); // ['#0000FF','#FFFFFF']
// Forcing language
Status::descriptions(null, 'it'); // 🇮🇹 ['In attesa','Accettato','Rifiutato','Nessuna Risposta']
StatusString::colors(null, 'it'); // ['#000000','#0000FF','#FF0000','#FFFFFF']
// Subset and language
Status::descriptions([Status::NO_RESPONSE, Status::DISCARDED], 'it'); // 🇮🇹 ['Nessuna Risposta', 'Rifiutato']
StatusString::colors([StatusString::ACCEPTED, StatusString::NO_RESPONSE], 'it'); // ['#0000FF','#FFFFFF']
```

static `[property]sByName()` method
-----------------------------------

[](#static-propertysbyname-method)

This dynamic method returns an associative array of \[case name =&gt; `property()` result\]. The name of the method is the plural of the property so if you are using `property` it will be `propertiesByName()`.

```
StatusString::descriptionsByName(); // ['PENDING' => 'Await decision', 'ACCEPTED' => 'Recognized valid',...
StatusString::colorsByName(); // ['PENDING' => '#000000','ACCEPTED' => '#0000FF',...
Status::descriptionsByName(); // ['PENDING' => 'Await decision', 'ACCEPTED' => 'Recognized valid',...
// Subset
StatusString::descriptionsByName([StatusString::DISCARDED, StatusString::ACCEPTED]); // ['DISCARDED' => 'No longer useful', 'ACCEPTED' => 'Recognized valid']
Status::descriptionsByName([Status::PENDING, Status::DISCARDED]); // ['PENDING' => 'Await decision', 'DISCARDED' => 'No longer useful']
// Forcing language
StatusString::descriptionsByName(null, 'it'); // 🇮🇹 ['P' => 'In attesa','A' => 'Accettato',...
// Subset and language
Status::descriptionsByName([Status::DISCARDED, Status::NO_RESPONSE], 'it'); // 🇮🇹 ['DISCARDED' => 'Rifiutato','NO_RESPONSE' => 'Nessuna Risposta',...
```

static `[property]sByValue()` method
------------------------------------

[](#static-propertysbyvalue-method)

This dynamic method returns an associative array of \[case value =&gt; `property()` result\] on `BackedEnum`, \[case name =&gt; `property()` result\] otherwise. The name of the method is the plural of the property so if you are using `property` it will be `propertiesByValue()`.

```
Status::descriptionsByValue(); // ['PENDING' => 'Await decision', 'ACCEPTED' => 'Recognized valid',...
StatusString::descriptionsByValue(); // ['P' => 'Await decision', 'A' => 'Recognized valid',...
StatusString::colorsByValue(); // ['P' => '#000000','A' => '#0000FF',...
// Subset
Status::descriptionsByValue([Status::PENDING, Status::DISCARDED]); // ['PENDING' => 'Await decision', 'DISCARDED' => 'No longer useful']
StatusString::descriptionsByValue([StatusString::DISCARDED, StatusString::ACCEPTED]); // ['D' => 'No longer useful', 'A' => 'Recognized valid']
StatusString::colorsByValue([Status::PENDING, Status::DISCARDED]); // ['P' => '#000000', 'D' => '#FF0000']
// Forcing language
StatusString::descriptionsByValue(null, 'it'); // 🇮🇹 ['P' => 'In attesa','A' => 'Accettato',...
// Subset and language
Status::descriptionsByValue([StatusString::DISCARDED, StatusString::NO_RESPONSE], 'it'); // 🇮🇹 ['DISCARDED' => 'Rifiutato','NO_RESPONSE' => 'Nessuna Risposta',...
```

###  Health Score

59

—

FairBetter than 99% of packages

Maintenance88

Actively maintained with recent releases

Popularity45

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 89.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~65 days

Recently: every ~97 days

Total

22

Last Release

62d ago

Major Versions

v0.7.0 → v1.0.02022-09-09

v1.1.1 → v2.0.02024-11-03

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/497169?v=4)[Alberto Peripolli](/maintainers/trippo)[@trippo](https://github.com/trippo)

---

Top Contributors

[![trippo](https://avatars.githubusercontent.com/u/497169?v=4)](https://github.com/trippo "trippo (90 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![Carnicero90](https://avatars.githubusercontent.com/u/78483736?v=4)](https://github.com/Carnicero90 "Carnicero90 (3 commits)")[![eppak](https://avatars.githubusercontent.com/u/1262964?v=4)](https://github.com/eppak "eppak (2 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/datomatic-laravel-enum-helper/health.svg)

```
[![Health](https://phpackages.com/badges/datomatic-laravel-enum-helper/health.svg)](https://phpackages.com/packages/datomatic-laravel-enum-helper)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[anahkiasen/former

A powerful form builder

1.4k1.4M14](/packages/anahkiasen-former)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
