Download the PHP package foxbytehq/laravel-backed-enums without Composer
On this page you can find all versions of the php package foxbytehq/laravel-backed-enums. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download foxbytehq/laravel-backed-enums
More information about foxbytehq/laravel-backed-enums
Files in foxbytehq/laravel-backed-enums
Package laravel-backed-enums
Short Description Supercharge your PHP8 backed enums with superpowers like localization support and fluent comparison methods.
License MIT
Homepage https://github.com/foxbytehq/laravel-backed-enums
Informations about the package laravel-backed-enums

This package supercharges your PHP8 backed enums with superpowers like localization support and fluent comparison methods.
Installation
Usage
Make Command
Creating a new Laravel Backed Enum is easy with the make:enum command.
Command:
Arguments:
{name}: The name of the enum class to be created (e.g., OrderStatus). The command will automatically append "Enum" to the name (e.g., OrderStatusEnum).{type?}: The underlying data type for the enum. Can be either --int --string or if not specified it will be a pure enum.{--force}: Overwrite the enum if it already exists. Example Usage:
To create an enum named OrderStatusEnum backed by integers:
To create an enum named OrderStatusEnum backed by strings:
To create a pure enum named OrderStatusEnum:
This will generate an OrderStatusEnums in the app/Enums directory.
Upgrade your existing enums
The enum you create must implement the BackedEnum interface and also use the IsBackedEnum trait.
The interface is required for Laravel to cast your enum correctly and the trait is what gives your enum its superpowers.
Enum value labels (Localization)
Create enums.php lang file and create labels for your enum values.
You may then access these localized values using the ->label() or ::labelFor() methods.
Additionally rendering the enum in a blade template will render the label.
If you do not specify a label in the lang file these methods will return the value assigned to the enum inside the enum file. e.g MILLIGRAMS label will be milligrams.
Meta data
Adding metadata allows you to return additional values alongside the label and values.
Create a withMeta method on your enum to add metadata.
If you do not specify a withMeta method, meta will be an empty array.
Other methods
options
Returns an array of all enum values with their labels and metadata.
Usage
returns
names
Returns an array of all enum values.
Usage
returns
values
Returns an array of all enum values.
Usage
returns
labels
Returns an array of all enum labels.
Usage
returns
map
Returns an array of all enum values mapping to their label.
Usage
returns
toArray
Returns an array of a single enum value with its label and metadata.
Usage
returns
toHtml
An alias of ::label(). Used to satisfy Laravel's Htmlable interface.
Usage
returns
toJson
Returns a json string represention of the toArray return value.
is/isA/isAn
Allows you to check if an enum is a given value. Returns a boolean.
Note
isA,isAnare just aliases foris.
Usage
isNot/isNotA/isNotAn
Allows you to check if an enum is not a given value. Returns a boolean.
Note
isNotAandisNotAnare just aliases forisNot.
Usage
isAny
Allows you to check if an enum is contained in an array. Returns a boolean.
Usage
isNotAny
Allows you to check if an enum is not contained in an array. Returns a boolean.
Usage
rule
The backed enums may be validated using Laravel's standard Enum validation rule - new Illuminate\Validation\Rules\Enum(VolumeUnitEnum::class).
This method a shortcut for the validation rule.
Usage
Other Classes
AsFullEnumCollection
This cast is similar to the Laravel built in AsEnumCollection cast but unlike the built-in will maintain the full toArray structure
when converting to json.
E.g. the Laravel built in AsEnumCollection cast will return the following json:
This cast will return
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
We welcome all contributors to the project.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-backed-enums with dependencies
illuminate/console Version ^12.0 | ^13.0
illuminate/contracts Version ^12.0 | ^13.0
spatie/laravel-package-tools Version ^1.18