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

ActiveLibrary

beingarslan/laravel-enum
========================

Simple, extensible and powerful enumeration implementation for Laravel.

00PHP

Since May 20Pushed 1y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

[![Laravel Enum](https://github.com/Arslan/laravel-enum/raw/master/branding/logo.svg?sanitize=true)](https://github.com/Arslan/laravel-enum/raw/master/branding/logo.svg?sanitize=true)

[![Packagist Stable Version](https://camo.githubusercontent.com/f14de5f36c0553c247c4450c0e22412d13f1607ced13ae96eb72afabfd5e863e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4172736c616e2f6c61726176656c2d656e756d2e7376673f7374796c653d666c61742d737175617265266c6162656c3d737461626c65)](https://packagist.org/packages/Arslan/laravel-enum)[![Packagist downloads](https://camo.githubusercontent.com/b1a8c79484b7ab35975c74dd752d43d54b3275af134960248b372e45af9fdd7e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4172736c616e2f6c61726176656c2d656e756d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Arslan/laravel-enum)[![MIT Software License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)

Using this library is no longer recommended
-------------------------------------------

[](#using-this-library-is-no-longer-recommended)

Using this library is no longer recommended, especially for new projects. PHP 8.1 supports enums natively.

See .

About Laravel Enum
------------------

[](#about-laravel-enum)

Simple, extensible and powerful enumeration implementation for Laravel.

- Enum key value pairs as class constants
- Full-featured suite of methods
- Enum instantiation
- Flagged/Bitwise enums
- Type hinting
- Attribute casting
- Enum artisan generator
- Validation rules for passing enum key or values as input parameters
- Localization support
- Extendable via Macros

Created by [Ben Sampson](https://sampo.co.uk)

Jump To
-------

[](#jump-to)

- [Guide](#guide)
- [Installation](#installation)
- [Migrate to Native PHP Enums](#migrate-to-native-PHP-enums)
- [Enum Library](enum-library.md)
- [Basic Usage](#basic-usage)
    - [Enum Definition](#enum-definition)
    - [Instantiation](#instantiation)
    - [Instance Properties](#instance-properties)
    - [Instance Casting](#instance-casting)
    - [Instance Equality](#instance-equality)
    - [Type Hinting](#type-hinting)
- [Flagged/Bitwise Enum](#flaggedbitwise-enum)
- [Attribute Casting](#attribute-casting)
- [Migrations](#migrations)
- [Validation](#validation)
- [Localization](#localization)
- [Customizing Descriptions](#customizing-descriptions)
    - [Customizing Class Description](#customizing-class-description)
    - [Customizing Value Descriptions](#customizing-value-descriptions)
- [Extending the Enum Base Class](#extending-the-enum-base-class)
- [Laravel Nova Integration](#laravel-nova-integration)
- [PHPStan Integration](#phpstan-integration)
- [Artisan Command List](#artisan-command-list)
- [Enum Class Reference](#enum-class-reference)
- [Stubs](#stubs)

Documentation for older versions
--------------------------------

[](#documentation-for-older-versions)

You are reading the documentation for `6.x`.

- If you're using **Laravel 8** please see the [docs for `4.x`](https://github.com/Arslan/laravel-enum/blob/v4.2.0/README.md).
- If you're using **Laravel 7** please see the [docs for `2.x`](https://github.com/Arslan/laravel-enum/blob/v2.2.0/README.md).
- If you're using **Laravel 6** or below, please see the [docs for `1.x`](https://github.com/Arslan/laravel-enum/blob/v1.38.0/README.md).

Please see the [upgrade guide](UPGRADE.md) for information on how to upgrade to the latest version.

Guide
-----

[](#guide)

I wrote a blog post about using laravel-enum:

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

[](#installation)

Requires PHP 8, and Laravel 9 or 10.

```
composer require Arslan/laravel-enum
```

Migrate to Native PHP Enums
---------------------------

[](#migrate-to-native-php-enums)

PHP 8.1 supports enums natively. You can migrate your usages of `Arslan\Enum\Enum` to native PHP enums using the following steps.

Make sure you meet the following requirements:

- PHP 8.1 or higher
- Laravel 10 or higher
- Rector 0.17 or higher, your `rector.php` includes all relevant files
- Latest version of this library

Depending on the size of your project, you may choose to migrate all enums at once, or migrate just a couple or one enum at a time.

- Convert all enums at once: `php artisan enum:to-native`
- Pass the fully qualified class name of an enum to limit the conversion: `php artisan enum:to-native "App\Enums\UserType"`

    This is necessary if any enums are used during the bootstrap phase of Laravel, the conversion of their usages interferes with Larastan and prevents a second run of Rector from working.

Review and validate the code changes for missed edge cases:

- See [Unimplemented](tests/Rector/Unimplemented)
- `Enum::coerce()`: If only values were passed, you can replace it with `tryFrom()`. If keys or instances could also be passed, you might need additional logic to cover this.
- `Enum::$description` and `Enum::getDescription()`: Implement an alternative.
- try/catch-blocks that handle `Arslan\Enum\Exceptions\InvalidEnumKeyException` or `Arslan\Enum\Exceptions\InvalidEnumMemberException`. Either catch the `ValueError` thrown by native enums, or switch to using `tryFrom()` and handle `null`.

Once all enums are converted, you can remove your dependency on this library.

Enum Library
------------

[](#enum-library)

Browse and download from a list of commonly used, community contributed enums.

[Enum library →](enum-library.md)

Basic Usage
-----------

[](#basic-usage)

### Enum Definition

[](#enum-definition)

You can use the following Artisan command to generate a new enum class:

```
php artisan make:enum UserType
```

Now, you just need to add the possible values your enum can have as constants.

```
