PHPackages                             foxbytehq/laravel-backed-enums - 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. foxbytehq/laravel-backed-enums

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

foxbytehq/laravel-backed-enums
==============================

Supercharge your PHP8 backed enums with superpowers like localization support and fluent comparison methods.

v3.1.2(3mo ago)465914[1 issues](https://github.com/foxbytehq/laravel-backed-enums/issues)[1 PRs](https://github.com/foxbytehq/laravel-backed-enums/pulls)MITPHPPHP ^8.3CI passing

Since Sep 19Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/foxbytehq/laravel-backed-enums)[ Packagist](https://packagist.org/packages/foxbytehq/laravel-backed-enums)[ Docs](https://github.com/foxbytehq/laravel-backed-enums)[ RSS](/packages/foxbytehq-laravel-backed-enums/feed)WikiDiscussions main Synced 3w ago

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

[![Banner Image](https://camo.githubusercontent.com/604a9e27f1dfbf9288f6c7672f26c9052db0b207a3637282a261a33ce5286f93/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304261636b6564253230456e756d732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d666f786279746568712532466c61726176656c2d6261636b65642d656e756d73267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53757065726368617267652b796f75722b504850382b6261636b65642b656e756d732b696e2b4c61726176656c2e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/604a9e27f1dfbf9288f6c7672f26c9052db0b207a3637282a261a33ce5286f93/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304261636b6564253230456e756d732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d666f786279746568712532466c61726176656c2d6261636b65642d656e756d73267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53757065726368617267652b796f75722b504850382b6261636b65642b656e756d732b696e2b4c61726176656c2e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1bab4aefa836f700f6c8716b8f1d773b39208a5a3f31a6eca8f069ed0f1e7a59/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f786279746568712f6c61726176656c2d6261636b65642d656e756d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxbytehq/laravel-backed-enums)[![Total Downloads](https://camo.githubusercontent.com/4558d5c8dbd92c5908d842fb51a5cabf81fdc9435199298fdd1826fb6be104eb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f786279746568712f6c61726176656c2d6261636b65642d656e756d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxbytehq/laravel-backed-enums)

This package supercharges your PHP8 backed enums with superpowers like localization support and fluent comparison methods.

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

[](#installation)

```
composer require foxbytehq/laravel-backed-enums
```

Usage
-----

[](#usage)

### Make Command

[](#make-command)

Creating a new Laravel Backed Enum is easy with the make:enum command.

#### Command:

[](#command)

```
php artisan make:enum {name} --string # or --int
```

#### Arguments:

[](#arguments)

- `{name}`: The name of the enum class to be created (e.g., OrderStatus). The command will automatically append "Enum" to the name (e.g., OrderStatusEnum).
- `{type?}`: The underlying data type for the enum. Can be either --int --string or if not specified it will be a pure enum.
- `{--force}`: Overwrite the enum if it already exists. Example Usage:

To create an enum named OrderStatusEnum backed by integers:

```
php artisan make:enum OrderStatus --int
```

To create an enum named OrderStatusEnum backed by strings:

```
php artisan make:enum OrderStatus --string
```

To create a pure enum named OrderStatusEnum:

```
php artisan make:enum OrderStatus
```

This will generate an OrderStatusEnums in the `app/Enums` directory.

### Upgrade your existing enums

[](#upgrade-your-existing-enums)

The enum you create must implement the `BackedEnum` interface and also use the `IsBackedEnum` trait. The interface is required for Laravel to cast your enum correctly and the trait is what gives your enum its superpowers.

```
use Foxbytehq\LaravelBackedEnums\BackedEnum;
use Foxbytehq\LaravelBackedEnums\IsBackedEnum;

enum VolumeUnitEnum: string implements BackedEnum
{
    use IsBackedEnum;

    case MILLIGRAMS = "milligrams";
    case GRAMS = "grams";
    case KILOGRAMS = "kilograms";
    case TONNE = "tonne";
}
```

### Enum value labels (Localization)

[](#enum-value-labels-localization)

Create enums.php lang file and create labels for your enum values.

```
// resources/lang/en/enums.php

return [
     VolumeUnitEnum::class => [
        VolumeUnitEnum::MILLIGRAMS->value => "mg",
        VolumeUnitEnum::GRAMS->value      => "g",
        VolumeUnitEnum::KILOGRAMS->value  => "kg",
        VolumeUnitEnum::TONNE->value      => "t"
     ]
];
```

You may then access these localized values using the `->label()` or `::labelFor()` methods.
Additionally rendering the enum in a blade template will render the label.

```
VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"
// in blade
{{ VolumeUnitEnum::KILOGRAMS }} // "kg"
```

If you do not specify a label in the lang file these methods will return the value assigned to the enum inside the enum file. e.g MILLIGRAMS label will be milligrams.

### Meta data

[](#meta-data)

Adding metadata allows you to return additional values alongside the label and values.

Create a withMeta method on your enum to add metadata.

```
public function withMeta(): array
{
    return match ($this) {
        self::MILLIGRAMS                => [
            'background_color' => 'bg-green-100',
            'text_color'       => 'text-green-800',
        ],
        self::GRAMS                     => [
            'background_color' => 'bg-red-100',
            'text_color'       => 'text-red-800',
        ],
        self::KILOGRAMS, self::TONNE    => [
            'background_color' => 'bg-gray-100',
            'text_color'       => 'text-gray-800',
        ],
        default                         => [
            'background_color' => 'bg-blue-100',
            'text_color'       => 'text-blue-800',
        ],
    };
}
```

If you do not specify a `withMeta` method, meta will be an empty array.

Other methods
-------------

[](#other-methods)

### options

[](#options)

Returns an array of all enum values with their labels and metadata.

#### Usage

[](#usage-1)

```
VolumeUnitEnum::options();
```

returns

```
[
    [
        'name'  => 'MILLIGRAMS',
        'value' => 'milligrams',
        'label' => 'mg',
        'meta'  => [
            'background_color' => 'bg-green-100',
            'text_color'       => 'text-green-800',
        ],
    ],
    [
        'name'  => 'GRAMS',
        'value' => 'grams',
        'label' => 'g',
        'meta'  => [
            'background_color' => 'bg-red-100',
            'text_color'       => 'text-red-800',
        ],
        ...
    ]
]
```

### names

[](#names)

Returns an array of all enum values.

#### Usage

[](#usage-2)

```
VolumeUnitEnum::names();
```

returns

```
[
    'MILLIGRAMS',
    'GRAMS',
    'KILOGRAMS',
    'TONNE',
]
```

### values

[](#values)

Returns an array of all enum values.

#### Usage

[](#usage-3)

```
VolumeUnitEnum::values();
```

returns

```
[
    'milligrams',
    'grams',
    'killograms',
    'tonne',
]
```

### labels

[](#labels)

Returns an array of all enum labels.

#### Usage

[](#usage-4)

```
VolumeUnitEnum::labels();
```

returns

```
[
    'mg',
    'g',
    'kg',
    't',
]
```

### map

[](#map)

Returns an array of all enum values mapping to their label.

#### Usage

[](#usage-5)

```
VolumeUnitEnum::map();
```

returns

```
[
    'MILLIGRAMS' => 'mg',
    'GRAMS'      => 'g',
    'KILOGRAMS'  => 'kg',
    'TONNE'      => 't',
]
```

### toArray

[](#toarray)

Returns an array of a single enum value with its label and metadata.

#### Usage

[](#usage-6)

```
VolumeUnitEnum::MILLIGRAMS->toArray();
```

returns

```
[
    'name'  => 'MILLIGRAMS',
    'value' => 'milligrams',
    'label' => 'mg',
    'meta'  => [
        'color'      => 'bg-green-100',
        'text_color' => 'text-green-800',
    ],
]
```

### toHtml

[](#tohtml)

An alias of ::label(). Used to satisfy Laravel's Htmlable interface.

#### Usage

[](#usage-7)

```
VolumeUnitEnum::MILLIGRAMS->toHtml();
```

returns

```
mg
```

### toJson

[](#tojson)

Returns a json string represention of the toArray return value.

### is/isA/isAn

[](#isisaisan)

Allows you to check if an enum is a given value. Returns a boolean.

> **Note**`isA`, `isAn` are just aliases for `is`.

#### Usage

[](#usage-8)

```
VolumeUnitEnum::MILLIGRAMS->is(VolumeUnitEnum::MILLIGRAMS); //true
VolumeUnitEnum::MILLIGRAMS->is('MILLIGRAMS');               //true
VolumeUnitEnum::MILLIGRAMS->is('invalid');                  //exception
```

### isNot/isNotA/isNotAn

[](#isnotisnotaisnotan)

Allows you to check if an enum is not a given value. Returns a boolean.

> **Note**`isNotA` and `isNotAn` are just aliases for `isNot`.

#### Usage

[](#usage-9)

```
VolumeUnitEnum::MILLIGRAMS->isNot(VolumeUnitEnum::GRAMS); //true
VolumeUnitEnum::MILLIGRAMS->isNot('GRAMS');               //true
VolumeUnitEnum::MILLIGRAMS->isNot('invalid');             //exception
```

### isAny

[](#isany)

Allows you to check if an enum is contained in an array. Returns a boolean.

#### Usage

[](#usage-10)

```
VolumeUnitEnum::MILLIGRAMS->isAny(['GRAMS', VolumeUnitEnum::TONNE]);                    // false
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // true
```

### isNotAny

[](#isnotany)

Allows you to check if an enum is not contained in an array. Returns a boolean.

#### Usage

[](#usage-11)

```
VolumeUnitEnum::MILLIGRAMS->isNotAny(['GRAMS', VolumeUnitEnum::TONNE]);                    // true
VolumeUnitEnum::MILLIGRAMS->isNotAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
```

### rule

[](#rule)

The backed enums may be validated using Laravel's standard Enum validation rule - `new Illuminate\Validation\Rules\Enum(VolumeUnitEnum::class)`.
This method a shortcut for the validation rule.

#### Usage

[](#usage-12)

```
public function rules(): array
{
    return [
        'volume_unit' => [VolumeUnitEnum::rule()],
    ];
}

```

Other Classes
-------------

[](#other-classes)

### AsFullEnumCollection

[](#asfullenumcollection)

This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built-in will maintain the full `toArray` structure when converting to json.

E.g. the Laravel built in `AsEnumCollection` cast will return the following json:

```
[
    "MILLIGRAMS",
    "GRAMS"
]
```

This cast will return

```
[
    {
        "name": "MILLIGRAMS",
        "value": "MILLIGRAMS",
        "label": "mg",
        "meta": {
            "background_color": "bg-green-100",
            "text_color": "text-green-800"
        }
    },
    {
        "name": "GRAMS",
        "value": "GRAMS",
        "label": "g",
        "meta": {
            "background_color": "bg-red-100",
            "text_color": "text-red-800"
        }
    }
]
```

Changelog
---------

[](#changelog)

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

Contributing
------------

[](#contributing)

We welcome all contributors to the project.

License
-------

[](#license)

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

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance84

Actively maintained with recent releases

Popularity31

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~64 days

Recently: every ~96 days

Total

21

Last Release

93d ago

Major Versions

1.2.3 → v2.0.02023-08-28

v2.6.1 → v3.0.02026-03-23

PHP version history (2 changes)v1.0.0PHP ^8.1

v3.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/89a3fc3149eeb34725cdbe81bee9adc4a9db23425ef3595a3a115a4f23c813ea?d=identicon)[foxbytehq](/maintainers/foxbytehq)

---

Top Contributors

[![hailwood](https://avatars.githubusercontent.com/u/709773?v=4)](https://github.com/hailwood "hailwood (37 commits)")[![Jim-Webfox](https://avatars.githubusercontent.com/u/99769087?v=4)](https://github.com/Jim-Webfox "Jim-Webfox (29 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (23 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (12 commits)")[![SuryaWebfox](https://avatars.githubusercontent.com/u/53416793?v=4)](https://github.com/SuryaWebfox "SuryaWebfox (8 commits)")[![Guesswhoitis](https://avatars.githubusercontent.com/u/63756285?v=4)](https://github.com/Guesswhoitis "Guesswhoitis (4 commits)")[![Ezekiel-Webfox](https://avatars.githubusercontent.com/u/114041728?v=4)](https://github.com/Ezekiel-Webfox "Ezekiel-Webfox (3 commits)")[![csoutham](https://avatars.githubusercontent.com/u/576413?v=4)](https://github.com/csoutham "csoutham (2 commits)")[![vikas020807](https://avatars.githubusercontent.com/u/94953593?v=4)](https://github.com/vikas020807 "vikas020807 (1 commits)")[![foxbyte-developers](https://avatars.githubusercontent.com/u/12959301?v=4)](https://github.com/foxbyte-developers "foxbyte-developers (1 commits)")[![adelf](https://avatars.githubusercontent.com/u/2818394?v=4)](https://github.com/adelf "adelf (1 commits)")

---

Tags

enumlaravelphplaravellaravel-backed-enumsfoxbytehq

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/foxbytehq-laravel-backed-enums/health.svg)

```
[![Health](https://phpackages.com/badges/foxbytehq-laravel-backed-enums/health.svg)](https://phpackages.com/packages/foxbytehq-laravel-backed-enums)
```

###  Alternatives

[spatie/laravel-health

Monitor the health of a Laravel application

87411.3M153](/packages/spatie-laravel-health)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.3M42](/packages/spatie-laravel-pdf)[laravel/ai

The official AI SDK for Laravel.

9782.1M162](/packages/laravel-ai)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

328482.0k25](/packages/codewithdennis-filament-select-tree)[nativephp/desktop

NativePHP for Desktop

38133.6k8](/packages/nativephp-desktop)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3913.7k](/packages/rawilk-profile-filament-plugin)

PHPackages © 2026

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