PHPackages                             cerbero/laravel-enum - 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. cerbero/laravel-enum

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

cerbero/laravel-enum
====================

Laravel package to supercharge enum functionalities.

2.2.0(10mo ago)18989.6k↑24.6%19MITPHPPHP ^8.1CI passing

Since Apr 19Pushed 10mo ago3 watchersCompare

[ Source](https://github.com/cerbero90/laravel-enum)[ Packagist](https://packagist.org/packages/cerbero/laravel-enum)[ Docs](https://github.com/cerbero90/laravel-enum)[ GitHub Sponsors](https://github.com/cerbero90)[ Fund](https://ko-fi.com/cerbero90)[ RSS](/packages/cerbero-laravel-enum/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (10)Versions (12)Used By (0)

🎲 Laravel Enum
==============

[](#-laravel-enum)

[![Author](https://camo.githubusercontent.com/03d9f58e6572fecfcf84799a8c87025dffcbc1c384734566c8415b359142fc7c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d6365726265726f39302d626c75653f6c6f676f3d78267374796c653d666f722d7468652d6261646765266c6f676f53697a653d6175746f)](https://x.com/cerbero90)[![PHP Version](https://camo.githubusercontent.com/bd60b0b617a72184f87c60a662c16c30a8da63b82e5ba2ab6e9a7e6ca21fb9bc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6365726265726f2f6c61726176656c2d656e756d3f636f6c6f723d253233373737424234266c6f676f3d706870267374796c653d666f722d7468652d6261646765266c6f676f53697a653d6175746f)](https://www.php.net)[![Laravel Version](https://camo.githubusercontent.com/b8091056f39455231e1756078bfc850ac8f4c36f53279f095e381e213964b1b6/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d6c61726176656c266d6573736167653d254532253839254135392e3026636f6c6f723d666632643230266c6f676f3d6c61726176656c267374796c653d666f722d7468652d6261646765266c6f676f53697a653d6175746f)](https://laravel.com)[![Latest Version](https://camo.githubusercontent.com/ff1a7f54fd61dc44f491d181c3248cd4515507db99253a79cb07bccc45fe3189/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6365726265726f2f6c61726176656c2d656e756d2e7376673f6c6162656c3d76657273696f6e267374796c653d666f722d7468652d6261646765266c6f676f3d766974657373266c6f676f436f6c6f723d666666266c6f676f53697a653d6175746f)](https://packagist.org/packages/cerbero/laravel-enum)[![Software License](https://camo.githubusercontent.com/9ea7157111981e64ce15546f9a0acc7710430886a175c7c143eec8b6b26da197/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d6c65726e61266c6f676f436f6c6f723d666666266c6f676f53697a653d6175746f)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/5a21920e55d035fde065eb1870586c14eb9e7c8c258662033d5a67438babe647/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6365726265726f39302f6c61726176656c2d656e756d2f6275696c642e796d6c3f6272616e63683d6d6173746572267374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6f676f53697a653d6175746f)](https://github.com/cerbero90/laravel-enum/actions?query=workflow%3Abuild)[![Code Quality](https://camo.githubusercontent.com/58f9561a2b65b365701868496f39528a3c452579ad8a73df04599b4f05c4ddd3/68747470733a2f2f696d672e736869656c64732e696f2f636f646163792f67726164652f65663531306236623161313834616339393931663465303464306538653735333f7374796c653d666f722d7468652d6261646765266c6f676f3d636f64616379266c6f676f53697a653d6175746f)](https://app.codacy.com/gh/cerbero90/laravel-enum/dashboard)[![Coverage](https://camo.githubusercontent.com/5b4fb9ab2410265cbadae1f1f56fd80755923a2e07cc72c231c7f96e4d9d2b40/68747470733a2f2f696d672e736869656c64732e696f2f636f646163792f636f7665726167652f65663531306236623161313834616339393931663465303464306538653735333f7374796c653d666f722d7468652d6261646765266c6f676f3d636f64616379266c6f676f53697a653d6175746f)](https://app.codacy.com/gh/cerbero90/laravel-enum/dashboard)[![PHPStan Level](https://camo.githubusercontent.com/047424e0daa5f9eb89165511c840728f3626285cc1c865418f2367831b7f510c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068707374616e2d6d61782d737563636573733f7374796c653d666f722d7468652d6261646765266c6f676f3d646174613a696d6167652f706e673b6261736536342c6956424f5277304b47676f414141414e53556845556741414143414141414167434159414141427a656e72304141414762306c455156523432753158653142555a52532f79344b67386f6952334643434255795345535a4252436961426e6d45734f7a65537a73672b4b7859594f3964454566744e52715a6a78343046525a6b5470716d4f7a3553324c73586c455a42636961746b516e484447596147644679314570474d486c2f702f5064466c7432726b354f2b4a396e356e412f767466356e6564336c6e6c49537052686166426c4c524c4843744a475672422f5a4244736177326c55717a526547414334364473745459666e534347556a61614476677841436f366a337655656e4e64496d65525871646e575635617a3572726e7a655a7a6e6a384a2b4535467473636c68663373344a3443532f6f52783542766f6e385a553635464759517841776366383561374365527a2b4334315448656a75657964435a3741414b33346e7776336b48502f6f554b644f4c344b373235386646374375643432374f3438525165476b49474a37374e38665a716c726366525034642f78393057516648584c65427439645472536c776c33563635796e574c4d315345413271624e51636b626534586d77773130486d79337368696430434d636d6c454a745344736c35565a42646641674d7649337575522b6d6f4a714e364c61786d70734f42654c43446d546966434239325263516d6241554a767471414c63357351723870383667594243634664427139774f696e374e5161783665776c423672714c5a486632334650313079336c6a36754a74454267324878695643747a64335345774d4243696f364e6839757a5a344f2f764c774f5a344f554e4d324e79494750467276757a42472f2f6c5250732b5651326b316b692b65506b64383462736b7a375946705967697a457a3838503876507a596666753364445330674a4e545531515856304e71616d70524b31574977676669453471684f7969673072432b7043764b3851556f4d4c37754a564841356b635155703344537071576a6333642f4479386f4b696f694c6f367571436f614568754862314b765430394141684246706257346c4f70794d797949425153436d6f55514c517a676e694e767a2b6f624232485332527742674536644f7843794a6f676d4e6b503275315772687734514a30332b69477252395845643343544e426e366543626f3430775044774d645856314246314456473571694574626f78535550364a37312b44334e775541684c4f4952517a6d376c6e6e68595576375146762f79445a2f4c6d3575624b3244564939695a386252384a4474454235376c4e7a454e514e364f6a6f49476c70616249565a7359614d544f2b6872696b525241314a786d5358396845372f734a745679463338744b735543565a7842687a396a49337747542f514a6c41447a50417958726e6a306b496e7a47485143524d794f672f65643275486a784975453454675951487132444c4a71756d617368592b6c6e734d433447564335646f365856754b396c2b34536b4e38792b47665965564a6e32672b2b553751796750543064426759474944765435386d6e46355051636a433833507a5346396648375331745a47456841515a514f54384a61413331376f496b4d366a533875564c53447a4f517167323355682b4d6c6b4f66303047673063503334632b7676373455527a4d396e34316762792f7276766b63374f54686c415455334e4347594a5558743451614c75545977426354534f426d6a315244374434547369783442794f6a5a52462f7a677570444562675a336a346c792f71656b704e44306f35615134344853344f416773567174493167545a4f303149624730615031626b6e6e7843445576417248692b42306c4a536c7a676c5446594f3275644633516c39544372486e356f45497265487036516c5255464a53556c4a43717169705357566c4a38764c79434759494653374853337a476138376d76346c636a4c774c6c53746c4c544b59595555416c76726c444763573435774b785858366171485a4e75744d2b316f51424846546577414b6b6f48342b7671436a3438505941475335796235616d6a4e6f4f2b435532534c35334e4b70444430767848486d4f4a6972374c357855765a676d307573325231343253634f4979567159766c70575534586f4849503844584c32622b776a6457655868365532466a6d49494b6d625741595046524d75733632682f676549766a4f51596c707544797351724c4c364765723439486757386a7176585568493755764462396961535444714874794974694635537577356577462f4e6438564a367a6c68736e30366245687758344e79664376754745655270546d68346d6b47363879447079757a42394555636a553561776241676e63506c4165536441514552307a436e647a715662655843347144734d70764745594258526e734478344e33417566314643546a5449615674592f51546d643049386242566d316b656a45756255664f30317671496d6e336334395837717065714939696e4967746270784b3359724b66494a43742b4f6556326e665556465234636134456b56454e794137676b59634d66423152354d4d6d785a37657a2f324b463553534e3179562b3135385550734a54305a4263493262524c744958476f5975354665724f55694a65314f66734c335845574834336c324b532b694a46392b53344670634e6773632b6a3863543848346f31626650672f716b4c743530754a31527a644d7347673055717766454e313134507762314374575447672b59395535436c4b397837785557493742493556515670304156635133625a6b51686d6e45676448684b794e535a65313663727442496c63377349623663524c6674325043676f4b476a696a4244746a72415137613345644d73787a4952666c414649685062366d48596d5977582b57426c505167736b6867567279794a4351794e79424c73425164513666677351687974364d534f4f73575a37676248387745546d67524b41696a61744e4c384e676d30787834744c6373707330577a7834616c306a586c493430422f413370613134344d447453674141414141456c46546b5375516d4343266c6f676f53697a653d6175746f)](https://phpstan.org/)[![Total Downloads](https://camo.githubusercontent.com/9aaca3bd47639a587f12f27012a987b763aae6828d2be4bbc4ab920cc1205e04/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6365726265726f2f6c61726176656c2d656e756d2e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d7061636b6167697374266c6f676f53697a653d6175746f)](https://packagist.org/packages/cerbero/laravel-enum)

Laravel package to supercharge enum functionalities.

Tip

Need a framework-agnostic solution? Consider using [🎲 Enum](https://github.com/cerbero90/enum) instead.

📦 Install
---------

[](#-install)

Via Composer:

```
composer require cerbero/laravel-enum
```

🔮 Usage
-------

[](#-usage)

- [🏷️ Meta](#%EF%B8%8F-meta)
- [🧺 Cases collection](#-cases-collection)
- [🪄 Magic translation](#-magic-translation)
- [💊 Encapsulation](#-encapsulation)
    - [🗄️ Cache](#%EF%B8%8F-cache)
    - [🔓 Session](#-session)
- [🦾 Artisan commands](#-artisan-commands)
    - [🗒️ enum:annotate](#%EF%B8%8F-enumannotate)
    - [🏗️ enum:make](#%EF%B8%8F-enummake)
    - [💙 enum:ts](#-enumts)

This package provides all the functionalities of [🎲 Enum](https://github.com/cerbero90/enum) plus Laravel-specific features.

To supercharge our enums, we just need to let them use the `Enumerates` trait:

```
use Cerbero\LaravelEnum\Concerns\Enumerates;

enum Numbers: int
{
    use Enumerates;

    case One = 1;
    case Two = 2;
    case Three = 3;
}
```

### 🏷️ Meta

[](#️-meta)

Laravel Enum extends [Enum's meta](https://github.com/cerbero90/enum?tab=readme-ov-file#%EF%B8%8F-meta) by allowing us to attach meta with a class name. This class is resolved by the Laravel container when invoking the corresponding meta method:

```
use Cerbero\Enum\Attributes\Meta;

enum PayoutStatuses
{
    use Enumerates;

    #[Meta(handler: SentPayoutHandler::class)]
    case Sent;

    #[Meta(handler: OnHoldPayoutHandler::class)]
    case OnHold;

    #[Meta(handler: DeclinedPayoutHandler::class)]
    case Declined;
}
```

In the enum above, each case specifies a `handler` meta with a class name. When a case calls its `handler()` meta method, the corresponding class is resolved through the IoC container:

```
// 🐢 instead of this
$handler = match ($payout->status) {
    PayoutStatuses::Sent => SentPayoutHandler::class,
    PayoutStatuses::OnHold => OnHoldPayoutHandler::class,
    PayoutStatuses::Declined => DeclinedPayoutHandler::class,
};

return Container::getInstance()->make($handler);

// 🐇 we can do this
return $payout->status->handler();
```

If we need to resolve a default class for most cases, we can attach the meta to the enum itself. Cases with their own meta override the default class:

```
use Cerbero\Enum\Attributes\Meta;

#[Meta(handler: DefaultPayoutHandler::class)]
enum PayoutStatuses
{
    use Enumerates;

    #[Meta(handler: SentPayoutHandler::class)]
    case Sent;

    case OnHold;

    case Declined;
}
```

In the example above, all cases calling the `handler()` method resolve the `DefaultPayoutHandler` class, except for the `Sent` case that resolves `SentPayoutHandler`.

If the class to be resolved is callable (i.e. it implements the `__invoke()` method), that class will be both resolved and executed:

```
use Cerbero\Enum\Attributes\Meta;

enum PayoutStatuses
{
    use Enumerates;

    #[Meta(handlePayout: SentPayoutHandler::class)]
    case Sent;

    #[Meta(handlePayout: OnHoldPayoutHandler::class)]
    case OnHold;

    #[Meta(handlePayout: DeclinedPayoutHandler::class)]
    case Declined;
}
```

In the enum above, each case specifies a `handlePayout` meta with a callable class. Calling `handlePayout()` resolves and invokes the class with any passed parameters.

```
// 🐢 instead of this
$handler = match ($payout->status) {
    PayoutStatuses::Sent => SentPayoutHandler::class,
    PayoutStatuses::OnHold => OnHoldPayoutHandler::class,
    PayoutStatuses::Declined => DeclinedPayoutHandler::class,
};

$handlePayout = Container::getInstance()->make($handler);

return $handlePayout($payout);

// 🐇 we can do this
return $payout->status->handlePayout($payout);
```

If we need to run a default callable class for most cases, we can attach the meta to the enum itself. Cases with their own meta override the default callable class:

```
use Cerbero\Enum\Attributes\Meta;

#[Meta(handlePayout: DefaultPayoutHandler::class)]
enum PayoutStatuses
{
    use Enumerates;

    #[Meta(handlePayout: SentPayoutHandler::class)]
    case Sent;

    case OnHold;

    case Declined;
}
```

In the example above, all cases calling the `handlePayout()` method execute the `DefaultPayoutHandler` class, except for the `Sent` case, which runs the `SentPayoutHandler`.

Tip

Our IDE can autocomplete meta methods thanks to the [`enum:annotate` command](#%EF%B8%8F-enumannotate).

Class names in meta are annotated by identifying the common interface or parent class they share.

### 🧺 Cases collection

[](#-cases-collection)

The [original cases collection](https://github.com/cerbero90/enum?tab=readme-ov-file#-cases-collection) has been extended for better integration with the Laravel framework.

The new cases collection implements the `Illuminate\Contracts\Support\Arrayable` and `Illuminate\Contracts\Support\Jsonable` contracts and it can be serialized into a JSON.

It also leverages the following Laravel traits:

- `Illuminate\Support\Traits\Conditionable` for conditional chaining of methods
- `Illuminate\Support\Traits\Macroable` for adding methods to the collection at runtime
- `Illuminate\Support\Traits\Tappable` for running custom logic while keeping method chaining

Additionally, the new collection enables us to `dump()` and `dd()` its cases:

```
Numbers::collect()->dump();

Numbers::collect()->dd();
```

Cases collection can be cast in an Eloquent model to store multiple cases in a single database column and then re-hydrate those cases back into a collection:

```
use Cerbero\LaravelEnum\CasesCollection;

class User extends Model
{
    // before Laravel 11
    protected $casts = [
        'numbers' => CasesCollection::class . ':' . Numbers::class,
    ];

    // after Laravel 11
    protected function casts(): array
    {
        return [
            'numbers' => CasesCollection::of(Numbers::class),
        ];
    }
}
```

Once the cast is set, we can assign an array of names, values or cases to the cast property of the model and receive a cases collection when accessing the property:

```
$user->numbers = ['One', 'Two'];

$user->numbers = [1, 2];

$user->numbers = [Numbers::One, Numbers::Two];

$user->numbers; // CasesCollection[Numbers::One, Numbers::Two]
```

The cases collection above is stored in the database as `["One","Two"]` for a pure enum, or as `[1,2]` for a backed enum.

The cast also supports bitwise backed enums, so for instance, if we have a `Permissions` enum implementing the `Bitwise` contract:

```
use Cerbero\LaravelEnum\Contracts\Bitwise;

enum Permissions: int implements Bitwise
{
    use Enumerates;

    case CreatePost = 1;
    case UpdatePost = 2;
    case DeletePost = 4;
}
```

and we cast the `permissions` property in our Eloquent model:

```
use Cerbero\LaravelEnum\CasesCollection;

class User extends Model
{
    // before Laravel 11
    protected $casts = [
        'permissions' => CasesCollection::class . ':' . Permissions::class,
    ];

    // after Laravel 11
    protected function casts(): array
    {
        return [
            'permissions' => CasesCollection::of(Permissions::class),
        ];
    }
}
```

we can assign a bitwise value or an array of bitwise values/cases to the `permissions` property and receive a cases collection in return:

```
$user->permissions = 3;

$user->permissions = 1 | 2;

$user->permissions = Permissions::CreatePost->value | Permissions::UpdatePost->value;

$user->permissions = [1, 2];

$user->permissions = [Permissions::CreatePost, Permissions::UpdatePost];

$user->permissions; // CasesCollection[Permissions::CreatePost, Permissions::UpdatePost]
```

The cases collection above is stored in the database as `3`, the result of the `OR` bitwise operator.

### 🪄 Magic translation

[](#-magic-translation)

On top of [Enum's magic](https://github.com/cerbero90/enum?tab=readme-ov-file#-magic), when a case calls an inaccessible method without a corresponding [meta](#%EF%B8%8F-meta) match, Laravel Enum assumes that we want to access a translation:

```
Numbers::One->description();

// lang/en/enums.php
return [
    Numbers::class => [
        'One' => [
            'description' => 'This is the case One.',
        ],
    ],
];
```

By default the translation key is resolved as `enums.{enum namespace}.{case name}.{inaccessible method}`. If customization is needed, we can adjust the translation key:

```
use Cerbero\LaravelEnum\Enums;

Enums::translateFrom(function(UnitEnum $case, string $method) {
    return sprintf('custom.%s.%s.%s', $case::class, $case->name, $method);
});
```

The above logic will resolve the translation key as `custom.{enum namespace}.{case name}.{inaccessible method}`.

Additionally, we can pass named arguments to replace placeholders within our translations:

```
return [
    Numbers::class => [
        'One' => [
            'description' => 'This is the case :value.',
        ],
    ],
];

// This is the case 1.
Numbers::One->description(value: 1);
```

Translations are accessible through [enum operations](https://github.com/cerbero90/enum?tab=readme-ov-file#-enum-operations) as well:

```
$options = Numbers::pluck('description', 'value');

/*
[
    1 => 'This is the case One.',
    2 => 'This is the case Two.',
    3 => 'This is the case Three.',
]
*/
```

Tip

Our IDE can autocomplete translation methods thanks to the [`enum:annotate` command](#%EF%B8%8F-enumannotate).

### 💊 Encapsulation

[](#-encapsulation)

Laravel Enum offers extra traits to encapsulate Laravel features that deal with keys. By defining keys as enum cases, we can leverage Laravel features without having to remember or repeat such keys.

The benefits of this approach are many:

- avoiding scattered, error-prone strings throughout the codebase
- preventing key misspellings
- enabling IDE autocompletion
- reviewing all application keys in a central location
- updating keys in one place rather than replacing all instances

#### 🗄️ Cache

[](#️-cache)

To encapsulate the Laravel cache, we can define a backed enum with all our cache keys and apply the `EnumeratesCacheKeys` trait:

```
use Cerbero\LaravelEnum\Concerns\EnumeratesCacheKeys;

enum CacheKeys: string
{
    use EnumeratesCacheKeys;

    case PostComments = 'posts.{int $postId}.comments';
    case Tags = 'tags';
    case TeamMemberPosts = 'teams.{string $teamId}.users.{string $userId}.posts';
}
```

The `EnumeratesCacheKeys` trait incorporates `Enumerates`, hence all the features of this package. We can now leverage all the Laravel cache methods by statically calling enum cases:

```
CacheKeys::Tags()->exists();
CacheKeys::Tags()->missing();
CacheKeys::Tags()->hasValue();
CacheKeys::Tags()->get($default);
CacheKeys::Tags()->pull($default);
CacheKeys::Tags()->put($value, $ttl);
CacheKeys::Tags()->set($value, $ttl);
CacheKeys::Tags()->add($value, $ttl);
CacheKeys::Tags()->increment($value);
CacheKeys::Tags()->decrement($value);
CacheKeys::Tags()->forever($value);
CacheKeys::Tags()->remember($ttl, $callback);
CacheKeys::Tags()->rememberForever($callback);
CacheKeys::Tags()->sear($callback);
CacheKeys::Tags()->flexible($ttl, $callback, $lock, $alwaysDefer)
CacheKeys::Tags()->forget();
CacheKeys::Tags()->delete();
CacheKeys::Tags()->lock($seconds, $owner);
CacheKeys::Tags()->restoreLock($owner);
```

When calling cases statically, we can pass parameters to resolve dynamic keys. Such parameters replace the `{...}` placeholders in the cache keys:

```
CacheKeys::TeamMemberPosts($teamId, $userId)->exists();
```

#### 🔓 Session

[](#-session)

To encapsulate the Laravel session, we can define a backed enum with all our session keys and apply the `EnumeratesSessionKeys` trait:

```
use Cerbero\LaravelEnum\Concerns\EnumeratesSessionKeys;

enum SessionKeys
{
    use EnumeratesSessionKeys;

    case CartItems = 'cart-items';
    case OnboardingStep = 'onboarding-step';
    case FormsData = 'forms.{int $formId}.data';
}
```

The `EnumeratesSessionKeys` trait incorporates `Enumerates`, hence all the features of this package. We can now leverage all the Laravel session methods by statically calling enum cases:

```
SessionKeys::CartItems()->exists();
SessionKeys::CartItems()->missing();
SessionKeys::CartItems()->hasValue();
SessionKeys::CartItems()->get($default);
SessionKeys::CartItems()->pull($default);
SessionKeys::CartItems()->hasOldInput();
SessionKeys::CartItems()->getOldInput($default);
SessionKeys::CartItems()->put($value);
SessionKeys::CartItems()->remember($callback);
SessionKeys::CartItems()->push($value);
SessionKeys::CartItems()->increment($amount);
SessionKeys::CartItems()->decrement($amount);
SessionKeys::CartItems()->flash($value);
SessionKeys::CartItems()->now($value);
SessionKeys::CartItems()->remove();
SessionKeys::CartItems()->forget();
```

When calling cases statically, we can pass parameters to resolve dynamic keys. Such parameters replace the `{...}` placeholders in the session keys:

```
SessionKeys::FormsData($formId)->exists();
```

Tip

Our IDE can autocomplete case static methods thanks to the [`enum:annotate` command](#%EF%B8%8F-enumannotate).

### 🦾 Artisan commands

[](#-artisan-commands)

A handy set of Artisan commands is provided out of the box to interact with enums seamlessly.

Some commands generate enums or related files. If we want to customize such files, we can publish their stubs:

```
php artisan vendor:publish --tag=laravel-enum-stubs
```

After publishing, the stubs can be modified within the `stubs/laravel-enum` directory, located at the root of our application.

Certain commands supports the `--all` option to reference all enums in our application. By default, enums are searched in the `app/Enums` directory, but we can scan other folders as well:

```
use Cerbero\LaravelEnum\Enums;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Enums::setPaths('app/Enums', 'domain/*/Enums');
    }
}
```

In the example above, enums are searched in the `app/Enums` directory and all `Enums` sub-directories within `domain`, such as `domain/Posts/Enums`, `domain/Users/Enums`, etc.

#### 🗒️ enum:annotate

[](#️-enumannotate)

IDEs can autocomplete case static methods, meta methods, translations, etc. by running the `enum:annotate` command:

```
php artisan enum:annotate
```

If we don't provide any argument, a prompt appears to choose which enums to annotate. Or, we can simply use the `--all` option to annotate all enums:

```
php artisan enum:annotate --all

php artisan enum:annotate -a
```

Alternatively, we can provide one or more enums directly to the `enum:annotate` command. Both slashes and quoted backslashes are acceptable for defining enum namespaces:

```
php artisan enum:annotate App/Enums/Permissions "App\Enums\PayoutStatuses"
```

Lastly, if we wish to overwrite existing method annotations on enums, we can include the `--force` option:

```
php artisan enum:annotate --force

php artisan enum:annotate -f
```

#### 🏗️ enum:make

[](#️-enummake)

The `enum:make` command allows us to create a new, automatically annotated enum with the cases we need:

```
php artisan enum:make
```

If no arguments are given, prompts will guide us through defining the enum namespace, backing type and cases. Otherwise, all these details can be typed via command line:

```
php artisan enum:make App/Enums/Enum CaseOne CaseTwo

php artisan enum:make "App\Enums\Enum" CaseOne CaseTwo
```

For creating backed enums, we can manually set custom values for each case:

```
php artisan enum:make App/Enums/Enum CaseOne=value1 CaseTwo=value2
```

Or, we can automatically assign values to cases by using the `--backed` option:

```
php artisan enum:make App/Enums/Enum CaseOne CaseTwo --backed=int0
```

The `--backed` option accepts these values:

- `int0`: assigns incremental integers starting from 0 (0, 1, 2...)
- `int1`: assigns incremental integers starting from 1 (1, 2, 3...)
- `bitwise`: assigns incremental bitwise values (1, 2, 4...)
- `snake`: assigns the case name in snake case (case\_one, case\_two...)
- `kebab`: assigns the case name in kebab case (case-one, case-two...)
- `camel`: assigns the case name in camel case (caseOne, caseTwo...)
- `lower`: assigns the case name in lower case (caseone, casetwo...)
- `upper`: assigns the case name in upper case (CASEONE, CASETWO...)

To overwrite an existing enum, we can include the `--force` option:

```
php artisan enum:make App/Enums/Enum CaseOne CaseTwo --force

php artisan enum:make App/Enums/Enum CaseOne CaseTwo -f
```

We can generate the TypeScript counterpart of the newly created enum by adding the `--typescript` option:

```
php artisan enum:make App/Enums/Enum CaseOne CaseTwo --typescript

php artisan enum:make App/Enums/Enum CaseOne CaseTwo -t
```

#### 💙 enum:ts

[](#-enumts)

The `ts` command converts enums to their TypeScript equivalents, ensuring backend and frontend synchronization:

```
php artisan enum:ts
```

If we don't provide any argument, a prompt appears to choose which enums to synchronize. Or, we can simply use the `--all` option to synchronize all enums:

```
php artisan enum:ts --all

php artisan enum:ts -a
```

Alternatively, we can provide one or more enums directly to the `enum:ts` command. Both slashes and quoted backslashes are acceptable for defining enum namespaces:

```
php artisan enum:ts App/Enums/Permissions "App\Enums\PayoutStatuses"
```

By default, enums are synchronized to `resources/js/enums/index.ts`, but this can be easily customized:

```
use Cerbero\LaravelEnum\Enums;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // custom static path
        Enums::setTypeScript('frontend/enums/index.ts');

        // custom dynamic path
        Enums::setTypeScript(function (string $enum) {
            $domain = explode('\\', $enum)[1];

            return "resources/js/modules/{$domain}/enums.ts";
        });
    }
}
```

As shown above, we can either define a static path for TypeScript enums or dynamically set the TypeScript path for an enum based on its namespace.

To update enums that have already been synchronized, we can use the `--force` option:

```
php artisan enum:ts App/Enums/Enum --force

php artisan enum:ts App/Enums/Enum -f
```

📆 Change log
------------

[](#-change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

🧪 Testing
---------

[](#-testing)

```
composer test
```

💞 Contributing
--------------

[](#-contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

🧯 Security
----------

[](#-security)

If you discover any security related issues, please email  instead of using the issue tracker.

🏅 Credits
---------

[](#-credits)

- [Andrea Marco Sartori](https://x.com/cerbero90)
- [All Contributors](../../contributors)

⚖️ License
----------

[](#️-license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance54

Moderate activity, may be stable

Popularity49

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 97.7% 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 ~252 days

Recently: every ~437 days

Total

10

Last Release

317d ago

Major Versions

1.3.1 → 2.0.02025-01-17

PHP version history (3 changes)v1.0.0PHP ^7.1

1.3.1PHP ^7.1|^8.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/596523?v=4)[Matteo Picciolini](/maintainers/cerbero)[@cerbero](https://github.com/cerbero)

---

Top Contributors

[![cerbero90](https://avatars.githubusercontent.com/u/5838106?v=4)](https://github.com/cerbero90 "cerbero90 (167 commits)")[![CupOfTea696](https://avatars.githubusercontent.com/u/7327050?v=4)](https://github.com/CupOfTea696 "CupOfTea696 (3 commits)")[![tecdynamics](https://avatars.githubusercontent.com/u/10241862?v=4)](https://github.com/tecdynamics "tecdynamics (1 commits)")

---

Tags

enumenumerationlaravellaravelenumenumeration

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  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)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[laravel/boost

Laravel Boost accelerates AI-assisted development by providing the essential context and structure that AI needs to generate high-quality, Laravel-specific code.

3.5k10.6M274](/packages/laravel-boost)[livewire/flux

The official UI component library for Livewire.

9475.0M86](/packages/livewire-flux)[laravel/reverb

Laravel Reverb provides a real-time WebSocket communication backend for Laravel applications.

1.6k9.4M48](/packages/laravel-reverb)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)

PHPackages © 2026

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