paulocjota/friendly-enum

FriendlyEnum is a PHP trait used to handle enum fields in Laravel framework in an easy way.

dev-main 2022-04-05 13:43 UTC

This package is auto-updated.

Last update: 2025-07-05 21:18:15 UTC


README

friendly-enum

AboutGetting startedAPI ReferenceTechnologiesAuthorLicense


About

FriendlyEnum is a PHP trait used to handle enum fields in Laravel framework in an easy way.

The problem it solves

You create an enum field in the migration file with user states for example. As the system is developed you also need the values in the views, in Form Requests / validators and many other places. Now a change is needed in the system to support one more state. Good luck remembering in each place that you need to change. This package tries to solve this problem.

Getting started

Install the package

composer require paulocjota/friendly-enum

Setting up

<?php

namespace App\Models;

use Paulocjota\FriendlyEnum;// <-- 1. Imports trait

class User extends Model
{
    use FriendlyEnum;// <-- 2. Utilizes trait on chosen model

    const ENUM_STATUS = [// <-- 3. Creates one or more constants prefixed with "ENUM_"
        'normal' => 'normal',
        'blocked' => 'bloqueado',
        'banned' => 'banido',
    ];
}

Use cases

  • In migration files. Ex.:
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        // ...
        $table->enum('status', User::getEnumKeys('status'));
    });
}
  • In blade component files. Ex.:
<x-select :options="\App\Models\User::getEnum('status')" />
  • In Form Request / Validators. Ex.:
'user.status' => [
    'required',
    Rule::in(User::getEnumKeysAsString('status')),
],

API Reference

Method Return Comment
getEnum(string $enum, bool $capitalize = false) array Returns an array with key value
getEnumKeys(string $enum) array Returns an array with only the keys
getEnumKeysAsString(string $enum) string Returns a string with all keys each one separated by a comma.
getEnumKey(string $enum, string $value) string Returns the key as a string passing the value.
getEnumValue(string $enum, string $key, bool $capitalize = false) string Returns the value as a string passing the key.

Technologies

FriendlyEnum was created with Laravel in mind but it is pure PHP and has no dependency, so you are free to use with whatever tools you want.

Author

paulocjota

License

The FriendlyEnum trait is open-sourced software licensed under the MIT license.