This package provides a set of traits you can use to extend the functionality of your enums in Laravel.
You can install the package via composer:
composer require outerweb/enum-helpersAdd the following trait to your backed enum to get collection support:
use Outerweb\EnumHelpers\HasCollectionSupport;
enum MyEnum: string
{
use HasCollectionSupport;
case Foo = 'foo';
case Bar = 'bar';
public function getLabel(): string
{
return match ($this) {
self::Foo => 'Foo label',
self::Bar => 'Bar label',
};
}
}This allows you to collect enum cases easily into a Laravel collection:
$collection = MyEnum::collect(); // Collection{'foo' => MyEnum::Foo, 'bar' => MyEnum::Bar}You can get a collection of enum values using the collect('value') method:
$values = MyEnum::collect('value'); // Collection{'foo' => 'foo', 'bar' => 'bar'}You can get a collection of return values of a function by passing the function name to the collect() method:
$mapped = MyEnum::collect('getLabel'); // Collection{'foo' => 'Foo label', 'bar' => 'Bar label'}Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.
