NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Enums for Laravel

Badge colours and presentation

Available from 1.1.0. For badges and status chips, an enum can give each case a colour by implementing RoundlyConsulting\Enums\Contracts\HasColor. presentation() then returns one case as an EnumPresentation DTO, and presentations() every case in declaration order:

use RoundlyConsulting\Enums\Attributes\TranslatedLabels;
use RoundlyConsulting\Enums\Contracts\HasColor;
use RoundlyConsulting\Enums\Helpers;

#[TranslatedLabels]
enum OrderStatus: string implements HasColor
{
    use Helpers;

    case Pending = 'pending';
    case Shipped = 'shipped';

    public function color(): string
    {
        return match ($this) {
            self::Pending => 'warning',
            self::Shipped => 'success',
        };
    }
}

OrderStatus::Shipped->presentation();     // EnumPresentation {value: 'shipped', label: 'Na ceste', color: 'success'}
OrderStatus::presentations();             // Collection<int, EnumPresentation>, every case in declaration order
OrderStatus::presentations()->toJson();   // [{"value":"pending","label":"…","color":"warning"}, …]
  • The colour is a free-form string your front end understands — a badge variant, a CSS class, a hex code. The package passes it through and never interprets it.
  • HasColor is an interface, not a trait method, so an enum that already has a color() method with another return type keeps working: it gets color: null in its presentations until it implements the interface.
  • presentation() uses the backed value (the case name on a pure enum) with its native type, so an int stays an int, and readable() for the label, so translated labels and readable() overrides apply. presentations() maps presentation() over every case.
  • Without HasColor, color is null.
  • options() keeps its {value, label, name} shape for selects; use presentations() when you need the colour.

The EnumPresentation DTO

MemberTypeValue
valuestring|intThe backed value with its native type — the case name on a pure enum.
labelstringreadable() — translated.
color?stringcolor() under HasColor, otherwise null.
toArray()array['value' => ..., 'label' => ..., 'color' => ...]
jsonSerialize()arraySame as toArray().

RoundlyConsulting\Enums\DataTransferObjects\EnumPresentation is a final readonly class implementing Arrayable and JsonSerializable: toArray() and json_encode() always give exactly value, label and color, in that order, so presentations() goes straight into a JSON response or an Inertia prop.

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.