Where to put/how to handle enums in Laravel?

PhpLaravelEnums

Php Problem Overview


Laravel has a <select> form helper which takes as input a dictionary. I like to keep the values for all of these in a central place. For example, I might have an enum that looks like this:

$phoneTypes = [
    'CELL' => "Cellular",
    'HOME' => "Home",
    'WORK' => "Work",
];

Which I want to use in both my view/template, and in the database:

Schema::create('customers', function (Blueprint $table) {
    $table->increments('id');
    $table->enum('pri_phone_type',array_keys($phoneTypes));
    ...
});
  1. Is there a recommended place to put these?
  2. Can I make them global so I can access them easily in all my views?

Php Solutions


Solution 1 - Php

 Update: PHP 8.1 has finally brought native support for enums! 拾

See more here:

https://stitcher.io/blog/php-enums https://php.watch/versions/8.1/enums https://www.php.net/manual/en/language.enumerations.php

My original answer below no longer applies, but if you're working with an older version of PHP...


Original answer

You have several options for handling enums. Before we look at a few though, I would first strongly encourage you not to use the DB enum column type.

Database enums are problematic for a number of reasons. I suggest reading this article for example:

http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

So with that let's look at a few other options.

Using Laravel config

Since you're using Laravel, one very simple option is to stick an array of options in a config file.

Say you create a new file config/enums.php with the following:

return [
    'phone_types' => [
        'CELL' => "Cellular",
        'HOME' => "Home",
        'WORK' => "Work",
    ]
];

You can now access config('enums.phone_types') anywhere in your code, including your Blade template.

Using a PHP package

@Banford's answer shows how to do basic enum-type behavior with class constants. If you like that approach, I recommend looking at this article and package which builds on this concept to provide strongly type enums:

https://stitcher.io/blog/php-enums

https://github.com/spatie/enum

You would create a class like this:

/**
 * @method static self cell()
 * @method static self home()
 * @method static self work()
 */
class PhoneTypes extends Enum
{
}

And now you can call PhoneTypes::home() in your app. Check out the documentation for that package to see how you can create a map of values, if you want.

Using DB relationships

If you really want to manage your options in the database, I'd create a separate phone_types database table and create a relationship with your customers table. This is still a much better option than using enum column type.

Solution 2 - Php

I disagree with the accepted answer here. I feel that enums can be very useful for this kind of thing. I prefer to treat enums as types, and implement the methods you need on the Enum base class to give you the functionality you need such as getting a dictionary.

My simple example below:

abstract class PhoneType extends Enum {
    const Cell = "Cellular";
    const Home = "Home";
    const Work = "Work";
}

abstract class Enum {
    static function getKeys(){
        $class = new ReflectionClass(get_called_class());
        return array_keys($class->getConstants());
    }
}

Example usage:

PhoneType::getKeys();

See https://stackoverflow.com/questions/254514/php-and-enumerations for further details and a more in depth example.

Solution 3 - Php

Building on @Banfords answer, with PHP7 constants can now be arrays:

class User extends Authenticatable
{
	/**
     * The possible genders a user can be.
	 */
    const GENDER = [
		'Male',
		'Female',
        'Unspecified'
    ];

...

Solution 4 - Php

In addition to @Banford's answer:

I have recently put together a package which makes working with enums in Laravel much nicer. It's a combination of various implementations I had found while researching how to do the very same thing (hence why I'm here).

https://github.com/BenSampo/laravel-enum

In this case, you could do something like the following:

final class PhoneTypes extends Enum
{
    const Cellular = 0;
    const Work = 1;
    const Home = 2;
}

The values can then be accessed using:

PhoneTypes::Work // 1

I would recommend always setting the values to integers and subsequently storing them in the DB as ints.

The base Enum class has methods for getting all keys and values as arrays. The package also features a couple of other benefits which may be useful in this case such as validation - so that a user couldn't add a non-existent value to the DB.

There's also a generator which is pretty handy.

I hope this comes in useful for someone.

Solution 5 - Php

Just had similar issue, for me Eloquent Accessors and mutators worked the best. For this question it would go like:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    /**
    * @var array
    */
    protected $phoneTypes = [
        'Cellular',
        'Home',
        'Work'
    ];

   /**
    * @param int $value
    * @return string|null
    */
    public function getPhoneTypeAttribute($value)
    {
        return Arr::get($this->phoneTypes, $value);
    }
}

Please note that in database you should save numeric values, where 0 is cell, 1 is home and 2 is work. Secondly it would be wise to use translations here instead protected property.

Solution 6 - Php

You should not use enum at all.

The official Laravel 5.1 documentation states:

>Note: Renaming columns in a table with a enum column is not currently supported.

It happens when you have a enum column in your database table. Whether you are trying to rename another column, or change another column to nullable, the bug will appear. It's an issue with Doctrine\DBAL.

>Unknown database type enum requested

Even with laravel 5.8, problem is not resolved.

I need to add that you will have the same problem when adding available options into enum column declaration.

It brings me to a conclusion that You should use enum with care. or even You should not use enum at all.

Here is an example of how difficult would it be adding available options into enum column declaration

say you have this:

Schema::create('blogs', function (Blueprint $table) {
    $table->enum('type', [BlogType::KEY_PAYMENTS]);
    $table->index(['type', 'created_at']);
...

and you need to make more types available

public function up(): void
{
    Schema::table('blogs', function (Blueprint $table) {
        $table->dropIndex(['type', 'created_at']);
        $table->enum('type_tmp', [
            BlogType::KEY_PAYMENTS,
            BlogType::KEY_CATS,
            BlogType::KEY_DOGS,
        ])->after('type');
    });

    DB::statement('update `blogs` as te set te.`type_tmp` = te.`type` ');

    Schema::table('blogs', function (Blueprint $table) {
        $table->dropColumn('type');
    });

    Schema::table('blogs', function (Blueprint $table) {
        $table->enum('type', [
            BlogType::KEY_PAYMENTS,
            BlogType::KEY_CATS,
            BlogType::KEY_DOGS,
        ])->after('type_tmp');
    });

    DB::statement('update `blogs` as te set te.`type` = te.`type_tmp` ');

    Schema::table('blogs', function (Blueprint $table) {
        $table->dropColumn('type_tmp');
        $table->index(['type', 'created_at']);
    });
}

Solution 7 - Php

Let's say you need a dropdown of choices

Try this:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    const PHONE_TYPES = [
        [
            'label' => 'Cellular',
            'value' => 'Cellular',
        ],
        [
            'label' => 'Home',
            'value' => 'Home',
        ],
        [
            'label' => 'Work',
            'value' => 'Work',
        ],
    ];


    public function getPhoneTypesLabelAttribute()
    {
        return collect(static::PHONE_TYPES)->firstWhere('value', $this->phone_types)['label'] ?? '';
    }
}

While in your controller, let's say under the create method do this:

public function create(Customer $customer)
{
    return response([
        'meta' => [
            'customer'   => Customer::get(['id', 'first_name']),
            'phone_types' => Customer::PHONE_TYPES,
        ],
    ]);
}

Then, in your blade template

dd($phone_types);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - PhpjszobodyView Answer on Stackoverflow
Solution 2 - PhpBanfordView Answer on Stackoverflow
Solution 3 - PhpKirill FuchsView Answer on Stackoverflow
Solution 4 - PhpBenSampoView Answer on Stackoverflow
Solution 5 - PhplchachurskiView Answer on Stackoverflow
Solution 6 - PhpYevgeniy AfanasyevView Answer on Stackoverflow
Solution 7 - PhpAkere AchuView Answer on Stackoverflow