lazerg/laravel-enum-pro
Laravel Enum Pro

A powerful trait that supercharges PHP 8.1+ enums with Laravel-friendly utilities. Get values, names, random cases, and form-ready options with a clean, fluent API.
Installation
composer require lazerg/laravel-enum-pro
Enum Example
enum DifficultyEnum: int
{
use \Lazerg\LaravelEnumPro\EnumPro;
case VERY_EASY = 1;
case EASY = 2;
case MEDIUM = 3;
case STRONG = 4;
case VERY_STRONG = 5;
}
Accessing Value
// 1
DifficultyEnum::VERY_EASY();
// 3
DifficultyEnum::MEDIUM();
// 5
DifficultyEnum::VERY_STRONG();
// 3
$enum = DifficultyEnum::MEDIUM;
$enum();
Accessing Name
// ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
DifficultyEnum::namesToArray();
// 'VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG'
DifficultyEnum::namesToString();
// Collection(['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG'])
DifficultyEnum::names();
// 'MEDIUM'
DifficultyEnum::nameOf(3);
Accessing Values
// [1, 2, 3, 4, 5]
DifficultyEnum::valuesToArray();
// '1,2,3,4,5'
DifficultyEnum::valuesToString();
// Collection([1, 2, 3, 4, 5])
DifficultyEnum::values();
// 1
DifficultyEnum::valueOf('VERY_EASY');
// 3 (case-insensitive)
DifficultyEnum::valueOf('medium');
// 5 (spaces converted to underscores)
DifficultyEnum::valueOf('Very strong');
Accessing Options
// [1 => 'Very Easy', 2 => 'Easy', 3 => 'Medium', 4 => 'Strong', 5 => 'Very Strong']
DifficultyEnum::optionsToArray();
// Collection([1 => 'Very Easy', 2 => 'Easy', 3 => 'Medium', 4 => 'Strong', 5 => 'Very Strong'])
DifficultyEnum::options();
// 'Very Strong'
DifficultyEnum::getOption(5);
// ['Medium', 'Very Strong']
DifficultyEnum::getOptions([3, 5]);
// [['value' => 1, 'display' => 'Very Easy'], ['value' => 2, 'display' => 'Easy'], ...]
DifficultyEnum::selectionsToArray();
// Collection([['value' => 1, 'display' => 'Very Easy'], ['value' => 2, 'display' => 'Easy'], ...])
DifficultyEnum::selections();
Accessing Random Value
// [3, 1] (random values)
DifficultyEnum::randomArray(2);
// 4 (single random value)
DifficultyEnum::randomFirst();
// Collection([2, 5, 1]) (random values)
DifficultyEnum::random(3);
Testing
./vendor/bin/pest
License
This package is open-sourced software licensed under the MIT license.
Related Packages
iteks/laravel-enum
A comprehensive Laravel package providing enhanced enum functionalities, includi...
22,056
25
marcin-orlowski/laravel-api-response-builder
Helps building nice, normalized and easy to consume Laravel REST API.
533,783
853
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v13.0.0 | ^8.1 | ^9.0| | MIT | |
| v0.7.0 | ^8.1 | ^9.0| | MIT | |
| v0.6.0 | ^8.1| | ^6.0| | MIT | |
| v0.5.2 | ^8.1| | ^6.0| | MIT | |
| v0.5.1 | ^8.1 | ^6.0| | MIT | |
| v0.5.0 | ^8.1 | ^6.0| | MIT | |
| v0.4.1 | ^8.1 | ^6.0| | MIT | |
| v0.4.0 | ^8.1 | ^6.0| | MIT | |
| v0.3.4 | ^8.1 | ^6.0| | MIT | |
| v0.3.3 | ^8.1 | ^6.0| | MIT | |
| v0.3.2 | ^8.1 | ^6.0| | MIT | |
| v0.3.1 | ^8.1 | ^6.0| | MIT | |
| v0.3.0 | ^8.1 | ^6.0| | MIT | |
| v0.2.1 | ^8.1 | ^6.0| | MIT | |
| v0.2.0 | ^8.1 | ^6.0| | MIT | |
| v0.1.1 | ^8.1 | ^9.0 | MIT | |
| v0.1.0 | ^8.1 | ^9.0 | MIT |