In this article, we will discuss the Implementation Laravel Enum Package. It is a wonderful resource for Laravel. As you know, Laravel provides lots of inbuilt features. Developers can create an open source package to return something to the Laravel community. Its created by Ben Sampson that adding support for creating enums in PHP. It also includes a generator for Laravel.
Laravel fresh setup where we create a test case for this package. If you are not familiar with the Laravel then you can start with the given articles.
- Understanding Laravel Middleware
- Custom User Email Verification / Activation Laravel
- Laravel Default Authentication
- Email Verification Laravel 5.7
Laravel Enum Package Features
- Enum key-value pairs as class constants
- A full-featured suite of methods
- Enum artisan generator
- Validation rules for passing enum key or values as input parameters
- Localization support
You can use the following composer command to install the package.
composer require bensampo/laravel-enum
Generating Enum Class
Package having the builtin artisan command to generate Enum class.
php artisan make:enum UserType
Laravel Enum Package example of what an Enum class looks like:
Heres the list of available methods and examples of how to use this package.
Enum Controller Validations
Laravel Enum package provides two way to apply the validations EnumValue and EnumKey.validate($request, [ 'user_type' => ['required', new EnumValue(UserType::class)], ]); // EnumKey Validations $this->validate($request, [ 'user_type' => ['required', new EnumKey(UserType::class)], ]);}...
Access Enum Value
You can use the following code snippet to get specific Enum value.UserType::Editor // Returns 2
In this article, we will discuss the Laravel Enum Package. Its a wonderful resource, you can check the official repository on GitHub. Hope you like this article. Please feel to add the comment if any query or submit your feedback 🙂
You may like:
If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee