Overriding enum labels | enum | Spatie

 SPATIE

enum
====

spatie.be/open-source

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

 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)

  Overriding enum labels
- [ 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 labels
======================

Enums can be given a label, you can do this by overriding the `labels` method. This label can be used to, for example, show a user-friendly list of available enums in a dropdown field.

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

You don't need to override all labels, the default label will be the enum's value. You can access an enum's label like so:

```
$status->label;
```

Note that `label` is a read-only property, it cannot be changed.

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

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

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
