Download the PHP package vixen/laravel-enums without Composer
On this page you can find all versions of the php package vixen/laravel-enums. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vixen/laravel-enums
More information about vixen/laravel-enums
Files in vixen/laravel-enums
Package laravel-enums
Short Description A Laravel package that enhances PHP enums.
License MIT
Informations about the package laravel-enums
Laravel Enums
A collection of composable traits and attributes for working with PHP backed enums in Laravel.
Requirements
- PHP 8.3+
- Laravel 11, 12, or 13
Installation
Usage
Add any combination of traits to your backed enum:
Traits
HasLabels
Provides label functionality using #[Label], #[LongLabel], and #[ShortLabel] attributes.
When no attribute is provided, a label is auto-generated from the case name:
Label type parameter
Instead of separate attributes, you can use the type parameter on #[Label]:
Fallback chain
When requesting a specific label type (e.g., longLabel()), the resolution order is:
- The exact attribute (
#[LongLabel]or#[Label(..., type: 'long')]) - The default
#[Label] - Auto-generated label from the case name
Translation support
#[Label] and #[LongLabel] values are passed through Laravel's __() helper, so you can use translation keys:
#[ShortLabel] returns the raw string without translation, since it's intended for abbreviations.
HasDescriptions
Provides description functionality using the #[Description] attribute. Useful for longer descriptive text alongside labels.
Returns null when no #[Description] attribute is present on a case.
Translation support
#[Description] values are passed through Laravel's __() helper, so you can use translation keys:
HasOptions
Returns label-value pairs as a Collection, useful for select dropdowns and form components. Requires the enum to provide a label() method (typically via HasLabels) and a description() method (typically via HasDescriptions).
Customizing fields with only
Both options() and descriptiveOptions() accept an optional only parameter to customize which fields appear in the returned arrays. The value field is always included.
Available fields: value, name, label, shortLabel, longLabel, description. An InvalidArgumentException is thrown for fields that don't exist on the enum.
HasValues
Returns a Collection of all backed values.
Filterable
Select or exclude specific cases by name. Returns an EnumCollection (see below), so you can chain toOptions() or toDescriptiveOptions() directly.
EnumCollection
A custom collection class extending Laravel's Collection, designed to hold enum cases and provide convenient conversion methods.
toOptions() requires the enum to use HasLabels. toDescriptiveOptions() additionally requires HasDescriptions. Both methods accept an optional only parameter — see HasOptions for details.
Returned automatically by Filterable::only() and Filterable::except().
Snakeable
Converts the case name to snake_case.
ToArray
Converts a single case to an associative array. Requires the enum to provide a label() method.
ToValue
Explicit accessor for the backed value.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Alex Torscho
- All Contributors
License
The MIT License (MIT). Please see License File for more information.