PHPackages                             isap-ou/laravel-enum-helpers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. isap-ou/laravel-enum-helpers

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

isap-ou/laravel-enum-helpers
============================

Adding some helpers functions to PHP8 native enums for laravel projects

1.5.1(1mo ago)52.0k↑46.2%2MITPHP

Since Oct 22Pushed 1mo agoCompare

[ Source](https://github.com/isap-ou/laravel-enum-helpers)[ Packagist](https://packagist.org/packages/isap-ou/laravel-enum-helpers)[ RSS](/packages/isap-ou-laravel-enum-helpers/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (10)Dependencies (2)Versions (19)Used By (0)

Laravel Enum Helpers
====================

[](#laravel-enum-helpers)

This package brings some helpers to native [PHP Enums](https://www.php.net/manual/en/language.enumerations.basics.php)

[![Laravel Enum Helpers](https://github.com/isap-ou/laravel-enum-helpers/raw/main/images/banner.jpg?raw=true)](https://github.com/isap-ou/laravel-enum-helpers/blob/main/images/banner.jpg?raw=true)[![Latest Version on Packagist](https://camo.githubusercontent.com/7f3e340077ab5449c9bf2d19b8216fcf08e3b33ba14afa8b4fc34dbb465f57e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f697361702d6f752f6c61726176656c2d656e756d2d68656c706572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/isap-ou/laravel-enum-helpers)[![Total Downloads](https://camo.githubusercontent.com/55099704192c446d8cfd7d4e1e6212ac70459805ffa6b27935796b22ddbe7e3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f697361702d6f752f6c61726176656c2d656e756d2d68656c706572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/isap-ou/laravel-enum-helpers)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require isap-ou/laravel-enum-helpers
```

You will most likely need to edit the extensive configuration, so you can publish the config file with:

```
php artisan vendor:publish --tag="enum-helpers-config"
```

Default config

```
return [
    'enum_locations' => [
        'app/Enums' => '\\App\\Enums\\',
    ],

    'label' => [
        'prefix' => null,
        'namespace' => null,
    ],

    'post_migrate' => true,

    'js_objects_file' => 'resources/js/enums.js',
];
```

1. `enum_locations` - path where enums located. Key is directory with enums, value - namespace for specified directory
2. `post_migrate` - enable or disable post migrate event listener
3. `js_objects_file` - path for generated js output
4. `label.prefix` - get default prefix for translations of enum fields
5. `label.namespace` - get default prefix for translations of enum namespace (when using as part of own package)

Available helpers
-----------------

[](#available-helpers)

### Migration helper

[](#migration-helper)

The way easy to add all enums to database column.

Just add to Enum trait `InteractWithCollection`

```
use IsapOu\EnumHelpers\Concerns\InteractWithCollection;

enum ExampleEnum: string
{

    use InteractWithCollection;

    case ENUM_ONE = 'enum_one';
    case ENUM_TWO = 'enum_two';
}
```

And in migration class

```
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('table_name', function (Blueprint $table){
            ...
            $table->enum('enum_column', ExampleEnum::values()->toArray());
            ...
        });
    }
}
```

### Update enum columns in DB

[](#update-enum-columns-in-db)

Artisan command allows update available(possible) values for specific enum column.

Modify enum:

```
use IsapOu\EnumHelpers\Contracts\UpdatableEnumColumns;

enum ExampleEnum: string implements UpdatableEnumColumns
{

    case ENUM_ONE = 'enum_one';
    case ENUM_TWO = 'enum_two';

    public static function tables(): array
    {
        return [
            'table_name' => 'enum_column'
        ];
    }
}
```

Set column defaults (optional)

You can instruct the migrator to set a **DEFAULT** for specific enum columns. If an enum class defines a static method `tableColumnDefaults()`, the command will append DEFAULT `` to the generated SQL for the matching `table_column` keys.

```
    /**
     * Map "table_column" => enum case.
     * The migrator will generate "... DEFAULT 'value>'".
     *
     * @return array
     */
    public static function tableColumnDefaults(): array
    {
        return [
            'orders_status'      => self::NEW,
            'order_items_status' => self::NEW,
        ];
    }
```

And run command

```
php artisan enum-helpers:migrate:enums
```

There is also a listener enabled by default that will run after a successful migration. To disable it edit `enum-helpers.php`:

```
