New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

nasyrov/laravel-enums

Laravel package for Enum implementation.
74,979 32
Install
composer require nasyrov/laravel-enums
Latest Version:v1.1.11
PHP:>=7.0
Laravel:5 – 8
License:MIT
Last Updated:Dec 29, 2020
Links: GitHub  ·  Packagist
Maintainer: nasyrov

Laravel Enums

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Laravel package for Enum implementation.

Requirements

Make sure all dependencies have been installed before moving on:

Install

Pull the package via Composer:

$ composer require nasyrov/laravel-enums

Register the service provider in config/app.php:

'providers' => [
    ...
    Nasyrov\Laravel\Enums\EnumServiceProvider::class,
    ...
]

Usage

Generate a new enum class via the command:

$ php artisan make:enum UserStatusEnum

Define the enum constants:

/**
 * @method static UserStatusEnum ACTIVE()
 * @method static UserStatusEnum INACTIVE()
 */
class UserStatusEnum extends Enum
{
    const ACTIVE = 10;
    const INACTIVE = 20;
}

To use the enum new up the instance or simply call via the static methods:

$status = new UserStatusEnum(UserStatusEnum::ACTIVE);
$status = UserStatusEnum::ACTIVE();

Type-hint the model accessor:

public function getStatusAttribute($attribute) {
    return new UserStatusEnum($attribute);
}

Type-hint the model mutator:

public function setStatusAttribute(UserStatusEnum $attribute) {
    $this->attributes['status'] = $attribute->getValue();
}

Validation:

$this->validate($request, [
    'status' => [
        'required',
        Rule::in(UserStatusEnum::values()),
    ],
]);

Localization:

use Nasyrov\Laravel\Enums\Enum as BaseEnum;

abstract class Enum extends BaseEnum
{
    /**
     * Get the enum labels.
     *
     * @return array
     */
    public static function labels()
    {
        return static::constants()
            ->flip()
            ->map(function ($key) {
                // Place your translation strings in `resources/lang/en/enum.php`
                return trans(sprintf('enum.%s', strtolower($key)));
            })
            ->all();
    }
}
<select name="status">
    @foreach (UserStatusEnum::labels() as $value => $label)
        <option value="{{ $value }}">
            {{ $label }}
        </option>
    @endforeach
</select>

Testing

$ composer lint
$ composer test

Security

If you discover any security related issues, please email inasyrov@ya.ru instead of using the issue tracker.

Credits

License

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

Related Packages

artisaninweb/laravel-enum

A provider for Enums in Laravel.

91,036 11
wboyz/laravel-enum

Base enum class with some features.

12,450 8
spatie/enum

PHP Enums

39,228,750 854
datomatic/nova-enum-field

A Laravel Nova PHP 8.1 enum field with filters

182,062 19
cerbero/laravel-enum

Laravel package to supercharge enum functionalities.

109,055 189

Version History

Version Released PHP Laravel License
v1.1.11 >=7.0 ^5.5|^6.0|^7.0|^8.0 MIT
1.1.10 >=7.0 ^5.5|^6.0|^7.0 MIT
v1.1.9 >=7.0 ^5.5|^6.0 MIT
v1.1.8 >=7.0 5.5.*|5.6.*|5.7.*|5.8.* MIT
v1.1.7 >=7.0 5.5.*|5.6.*|5.7.* MIT