richardDobron / laravel-db-enum-generator

Artisan command to generate Enum classes fetched from database.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Enum

Laravel package that introduces a new Artisan command to generate Enum classes fetched from database.

Requirements

  • PHP 5.6 or higher
  • Laravel 5.6 or higher

Install

Via Composer

$ composer require dobron/laravel-db-enum-generator --dev

Usage

Enums can be generated by calling the Artisan command make:enum and specifying the class or table name, and columns for key and value(s).

Id Slug Role
1 MANAGER Admin
2 CONTENT_CREATOR Editor
3 MODERATOR Moderator
4 ADVERTISER Advertiser
5 INSIGHTS_ANALYST Analyst
$ php artisan make:enum App\Models\UserRoleTypes --model=UserRole --id=Id --slug=Slug --title=Role
<?php

namespace App\Enums;

class UserRoleTypes
{
    public const MANAGER = 1;
    public const CONTENT_CREATOR = 2;
    public const MODERATOR = 3;
    public const ADVERTISER = 4;
    public const INSIGHTS_ANALYST = 5;

    public static function map(): array
    {
        return [
            static::MANAGER => 'Admin',
            static::CONTENT_CREATOR => 'Editor',
            static::MODERATOR => 'Moderator',
            static::ADVERTISER => 'Advertiser',
            static::INSIGHTS_ANALYST => 'Analyst',
        ];
    }
}

Options

Option Description Default
model Eloquent model class name
table (or) The database table name
path The path to generate enums in
id ID column name id
slug Slug column name slug
value Column(s) name for map separated by comma
force Create the class even if the enum already exists false

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

About

Artisan command to generate Enum classes fetched from database.

License:MIT License


Languages

Language:PHP 100.0%