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

Open source

Enums for Laravel

Install
composer require roundly-consulting/enums-for-laravel
Requires: PHP ^8.4 · Laravel ^12.0|^13.0

Overview

A single trait that gives your PHP enums the helpers every Laravel app ends up rewriting: readable, translatable labels; names, values and labels as collections; select-ready options; lookups by name or label; a validation rule that never drifts; random picks; expressive comparisons and fluent conditional callbacks. Add RoundlyConsulting\Enums\Helpers to a backed or pure enum and you’re done — no service provider, config, migrations or commands. Native to Laravel — built on its own Collection, Str and translator — MIT-licensed, with no dependency beyond Laravel itself.

What you get

Readable, translatable labels

readable() and label() headline the backed value — hot-news becomes “Hot News” — then look it up in Laravel’s translator.

Names, values and labels

One call each for every case’s name, backed value or label, as Laravel Collections in declaration order.

Select-ready options

A value => label map for Blade selects, or typed EnumOption DTOs (value, label, name) for Inertia, Vue and React.

Case lookups

Resolve a case by name or label — strict from* variants throw, tryFrom* return null — plus hasName() and hasValue().

Validation that never drifts

validationRule() builds the in: rule from the current cases, so adding a case never breaks a form.

Comparisons and callbacks

is(), isNot(), isIn(), isNotIn() and fluent whenIs* callbacks with an optional default branch.

One trait, zero setup

No service provider, config, migrations, commands or facade. Works on backed and pure enums — pure enums use the case name wherever a value is read.

Documentation

Installation

Install with Composer — there is no service provider, config, migration or command to publish or register.

Quick start

Add the Helpers trait to an enum and get labels, options, lookups, validation and comparisons in one line.

Backed and pure enums

What each helper returns on string-backed, int-backed and pure enums — and where pure enums fall back to the case name.

Readable labels

readable() and label() turn a case into a human-friendly label with Str::headline(), passed through Laravel’s translator.

Translating labels

Localize labels with JSON translation files, or override readable() on the enum for full control.

Names, values and labels

names(), values(), storable(), labels(), collect() and count() — every case as a Collection, in declaration order.

Select options

toOptions() and toArray() build a value => label map for Blade selects, form components, JSON and API responses.

Front-end options

options() returns typed EnumOption DTOs — value, label and name — the shape JavaScript, Inertia, Vue and React selects expect.

Case lookups

Resolve a case by name or readable label with fromName(), fromLabel() and their tryFrom* variants; check existence with hasName() and hasValue().

Validation

validationRule() builds a Laravel in: rule from values(), so the allow-list never drifts when cases change.

Equality checks

is(), isNot(), isIn() and isNotIn() compare cases by identity — readable conditions without manual === chains.

Conditional callbacks

whenIs(), whenIsNot(), whenIsIn() and whenIsNotIn() run a callback only when the condition matches, with an optional default.

Random cases & factories

random() picks a random case for model factories, seeders and demo data; collect() walks every case.

Exceptions

EnumException — thrown by fromName() and fromLabel() on a miss — extends InvalidArgumentException, with two named constructors.

API reference

Every static and instance method the Helpers trait adds — signature, return type and purpose.

Testing

Assert labels, options and validation for your own enums in Pest, add translation lines with Lang::addLines(), and run the package suite.

Requirements

PHP 8.4+ and Laravel 12 or 13 — no PHP extensions, no service provider, no config.

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.