Overriding enum values | enum | Spatie

 SPATIE

enum
====

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Enum](https://spatie.be/docs/enum/v3)  Usage  Overriding enum values

 Version   v3   v2   v1

 Other versions for crawler [v3](https://spatie.be/docs/enum/v3) [v2](https://spatie.be/docs/enum/v2) [v1](https://spatie.be/docs/enum/v1)

- [ Introduction ](https://spatie.be/docs/enum/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/enum/v3/postcardware)
- [ Installation and setup ](https://spatie.be/docs/enum/v3/installation-and-setup)
- [ Questions &amp; issues ](https://spatie.be/docs/enum/v3/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/enum/v3/changelog)
- [ About us ](https://spatie.be/docs/enum/v3/about-us)

Usage
-----

- [ Internal enum values ](https://spatie.be/docs/enum/v3/usage/2-internal-enum-values)
- [ Overriding enum values ](https://spatie.be/docs/enum/v3/usage/3-overriding-enum-values)
- [ Overriding enum labels ](https://spatie.be/docs/enum/v3/usage/4-overriding-enum-labels)
- [ Comparing enums ](https://spatie.be/docs/enum/v3/usage/5-comparing-enums)
- [ Laravel ](https://spatie.be/docs/enum/v3/usage/100-laravel)

 Overriding enum values
======================

By default, the enum value is its method name. You can however override it, for example if you want to store enums as integers in a database, instead of using their method name.

```
/**
 * @method static self draft()
 * @method static self published()
 * @method static self archived()
 */
class StatusEnum extends Enum
{
    protected static function values(): array
    {
        return [
            'draft' => 1,
            'published' => 2,
            'archived' => 3,
        ];
    }
}
```

An enum value doesn't have to be a string, as you can see in the example.

You may also return a closure to derive the enum value from the method name.

```
/**
 * @method static self issue()
 * @method static self pullRequest()
 */
class EventEnum extends Enum
{
    protected static function values(): Closure
    {
        return fn (string $name) => snake_case($name);
    }
}
```
