outerweb/enum-helpers

Traits to make working with Enums a breeze in Laravel.

Fund package maintenance!
Outerweb

Installs: 112

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 1

pkg:composer/outerweb/enum-helpers

v2.0.0 2025-10-07 19:39 UTC

This package is auto-updated.

Last update: 2025-10-13 08:58:57 UTC


README

Enum helpers

Enum helpers

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a set of traits you can use to extend the functionality of your enums in Laravel.

Table of Contents

Installation

You can install the package via composer:

composer require outerweb/enum-helpers

Usage

Collection support

Add 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}

Getting a collection of values

You can get a collection of enum values using the collect('value') method:

$values = MyEnum::collect('value'); // Collection{'foo' => 'foo', 'bar' => 'bar'}

Getting a collection of return values of a function

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'}

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.