Where to put/how to handle enums in Laravel?

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?


ANSWERS:


Personally I'd avoid an enum DB column type altogether. Database enum column types can be problematic, see here for one article on this topic.

Handling enums in the DB via relationships

If you really want to manage your list in the database, I'd create a separate phone_types database table and create a relationship with your customers table.

Handling enums in your code via Laravel config

It's also quite easy to manage your enums in code, and avoid the DB completely.

Since you're using Laravel, I think setting up a config file would be a viable option.

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

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

You can now hand that off to the form helper quite easily:

Form::select('phone', Config::get('enums.phone_types'));

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 PHP and Enumerations for further details and a more in depth example.


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'
    ];

...

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.



 MORE:


 ? if($ext == ('zip' || 'png')) { echo "Is it possible ?" }
 ? How to implement Enum like functionality in PHP?
 ? Configuration storage setup [file vs. database]
 ? soap enumeration value in php
 ? Enumerate NSDictionary with keys and objects, PHP style
 ? Dynamic Enumeration in PHP. Possible? Sensible?
 ? 2 independent events 3 times - enumerate the possible outcomes
 ? how to link radiobuttons with enumeration?
 ? Is there anyway to have various levels for php enumerations?
 ? Php Eloquent Enumeration