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

Abandoned → [foxbytehq/laravel-backed-enums](/?search=foxbytehq%2Flaravel-backed-enums)Library[Utility &amp; Helpers](/categories/utility)

webfox/laravel-backed-enums
===========================

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

V3.1.1(1mo ago)4515.4k↓40%4[1 PRs](https://github.com/foxbytehq/laravel-backed-enums/pulls)MITPHPPHP ^8.3CI failing

Since Sep 19Pushed 4mo ago2 watchersCompare

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

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

[![Banner Image](https://camo.githubusercontent.com/151e95c6fb89b9b52849af969c6ef0862e2b8d745d62ee8f5e4ef9f242d6db31/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304261636b6564253230456e756d732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d776562666f782532466c61726176656c2d6261636b65642d656e756d73267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53757065726368617267652b796f75722b504850382b6261636b65642b656e756d732b696e2b4c61726176656c2e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/151e95c6fb89b9b52849af969c6ef0862e2b8d745d62ee8f5e4ef9f242d6db31/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304261636b6564253230456e756d732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d776562666f782532466c61726176656c2d6261636b65642d656e756d73267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53757065726368617267652b796f75722b504850382b6261636b65642b656e756d732b696e2b4c61726176656c2e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

[![Latest Version on Packagist](https://camo.githubusercontent.com/84c719a1e08a9383864b2f92f45996d3e990d8cea01816346d8e64a601935cf7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f776562666f782f6c61726176656c2d6261636b65642d656e756d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/webfox/laravel-backed-enums)[![Total Downloads](https://camo.githubusercontent.com/0aa96648bae5733dc302ed0c165ef355c416b577b894eccd3a62a48e35b830d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f776562666f782f6c61726176656c2d6261636b65642d656e756d732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/webfox/laravel-backed-enums)

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

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

[](#installation)

```
composer require webfox/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 Webfox\LaravelBackedEnums\BackedEnum;
use Webfox\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

56

—

FairBetter than 98% of packages

Maintenance81

Actively maintained with recent releases

Popularity38

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity71

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

Recently: every ~96 days

Total

20

Last Release

54d 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 (33 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)")[![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)")[![SuryaWebfox](https://avatars.githubusercontent.com/u/53416793?v=4)](https://github.com/SuryaWebfox "SuryaWebfox (2 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)")[![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/webfox-laravel-backed-enums/health.svg)

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

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

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