dobron/laravel-db-enum-generator

Database enum generator for Laravel.

1.0.3 2023-09-04 19:39 UTC

This package is auto-updated.

Last update: 2024-01-29 18:21:22 UTC


README

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

📖 Requirements

  • PHP 7.0 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\Enums\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.