Blog Detail


Laravel Enum Pro -  An Enhanced Version of PHP Built-in Enum cover image

arrow_back Laravel Enum Pro - An Enhanced Version of PHP Built-in Enum

Finally, the support for Php Enums has been added in the PHP version 8.1. But as enums are new in php, we do not have some helpers to work with that easily. So with this amazing Laravel Enum Pro package, you have pretty more options for working with enums than built-in methods.

It is just trait Lazerg\LaravelEnumPro\EnumPro which must be added into an enum. So it means it uses a built-in enum class while enhancing it.


You can install this package via Composer by running this command in the terminal.

composer require lazerg/laravel-enum-pro


Create a new enum class and use our trait on it.

enum LevelTypes: int {
    use \Lazerg\LaravelEnumPro\EnumPro;

    case VERY_EASY = 1;
    case EASY = 2;
    case MEDIUM = 3;
    case STRONG = 4;
    case VERY_STRONG = 5


With default functions, if you want to get the value of the case you should write LevelTypes::VERY_EASY->value which is little long. With our package, you can get the value of the case, by just calling it statically.

LevelTypes::VERY_EASY() // 1


As you can see, names here VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG. To get all case names of enum. you can use these helper methods:

LevelTypes::names();         // Collection: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
LevelTypes::namesToArray();  // Array: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
LevelTypes::namesToString(); // String: VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG


As you can see, the values here are 1, 2, 3, 4, 5. Common usage is for: validate incoming request data.

LevelTypes::values();         // Collection: [1, 2, 3, 4, 5]
LevelTypes::valuesToArray();  // Array: [1, 2, 3, 4, 5]
LevelTypes::valuesToString(); // String: 1, 2, 3, 4, 5


Sometimes we need to get random values or values from the enum. This is mainly used in factories.

LevelTypes::random(int $count = 1);      // Collection of $count random values
LevelTypes::randomArray(int $count = 1); // Array of $count random values


While creating the admin panel, we always change the state of models. And basically, it is recommended to save all state types in the enum. So in the admin panel, we need to get all options of enum for select. That’s why we have options() method.

LevelTypes::options(); // Return collection of selectable options
LevelTypes::optionsToArray(); // Return array of selectable options

If you want to explore more about this package, you can visit its complete documentation and source code on Github.

Published at : 26-04-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project