outerweb / enum-helpers
Laravel traits to enhance php enums
v1.0.1
2024-03-12 20:41 UTC
Requires
- php: ^8.1
- laravel/framework: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.16
README
Enum Helpers
This package provides Laravel specific helpers for working with enums.
- \Illuminate\Support\Collection support via the
HasCollectionSupport
trait.
Installation
You can install the package via composer:
composer require outerweb/enum-helpers
Usage
Collection support
use Outerweb\EnumHelpers\HasCollectionSupport; enum Status: string { use HasCollectionSupport; const Open = 'open'; const Closed = 'closed'; public function label(): string { return match ($this->value) { self::Open => 'Open', self::Closed => 'Closed', }; } }
You can now get a collection of all th cases by calling:
Status::collect();
You can also get a collection of all the labels by calling:
Status::collect('label');
Suggestions?
If you have any suggestions for extra enum helpers, please create an issue or a pull request.
Laravel support
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
MIT License (MIT). Read the License File for more information.