Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NetoPVH's avatar

Laravel Enum

Hello everyone , I have an application that there are some data that do not want to register on the database but use enums . Have any of you could help me ... if there is a package for Laravel 5.2 that is used to create enums .

Thank you!

0 likes
10 replies
willvincent's avatar

enum is natively supported. In your migration, just add the column thusly:

  // ...
  
  // Enum schema format:
  // $table->enum($column_name, $allowed_values);
  $table->enum('fruit', ['apple', 'banana', 'cantalope']);

  // ...

You will probably want to add your own validation to prevent creating records will null values which would happen if you passed a non-allowed value to the enum column, but otherwise it already exists and works.

If you don't write that validation, then you'll end up with something like this, even though the field is set to disallow null values:

>>> App\Foobar::create(['fruit' => 'apple']);
=> App\Foobar {#640
     fruit: "apple",
     updated_at: "2016-03-11 18:27:12",
     created_at: "2016-03-11 18:27:12",
     id: 1,
   }
>>> App\Foobar::create(['fruit' => 'banana']);
=> App\Foobar {#647
     fruit: "banana",
     updated_at: "2016-03-11 18:27:15",
     created_at: "2016-03-11 18:27:15",
     id: 2,
   }
>>> App\Foobar::create(['fruit' => 'pear']);
=> App\Foobar {#640
     fruit: "pear",
     updated_at: "2016-03-11 18:27:21",
     created_at: "2016-03-11 18:27:21",
     id: 3,
   }


>>> App\Foobar::all()
=> Illuminate\Database\Eloquent\Collection {#646
     all: [
       App\Foobar {#649
         id: 1,
         created_at: "2016-03-11 18:27:12",
         updated_at: "2016-03-11 18:27:12",
         fruit: "apple",
       },
       App\Foobar {#651
         id: 2,
         created_at: "2016-03-11 18:27:15",
         updated_at: "2016-03-11 18:27:15",
         fruit: "banana",
       },
       App\Foobar {#652
         id: 3,
         created_at: "2016-03-11 18:27:21",
         updated_at: "2016-03-11 18:27:21",
         fruit: "",
       },
     ],
   }

Note the empty fruit value for record 3.

Also note: sqlite does not support enum, so if you're using sqlite, a varchar field will be created.

3 likes
NetoPVH's avatar

sorry, not explained right , I need to create a class.

Example:

class CitiesEnum{

const PVH = "Porto Velho"; const VIL = "Vilhena";

}

I give a foreach to display these records

willvincent's avatar

I see.. so in the database you want to store the three letter code, or whatever the constant names are, but on the frontend you want to display the actual constant values?

So, ENUM in the DB may or may not be necessary then, it would probably depend on whether or not your list of constants is going to grow, if it is I'd probably avoid the enum in the db.

Also, instead of constants this might be easier to handle with a private array. I'm just thinking about data moving the other way -- into the database rather than out of it. If this were handled with an array you can easily find the key with a particular value, or the value of a particular key. Not sure how easy it would be to determine the name of a constant with a particular value programatically.

But, for the sake of argument, lets say you're going to use constants. If those are defined in the relevant model, then you could define an accessor. Something like this, for example:

class Cities extends Model {

  const PVH = "Porto Velho";
  const VIL = "Vilhena";


  public function getCityAttribute($value) {
    if (defined(self::$value)) {
      return self::$value;
    }
  }
}

Then, when loading your models, city values stored in the DB as VIL should be in the result set as Vilhena and so forth.

Is that more along the lines of what your were looking for?

4 likes
jekinney's avatar

I avoid enum fields as all db don't support them an really don't help much, but to each his/her own.

You can either set another table and return that data, or hard code an array a return it. Very similar to countries list with abbreviation => full name

1 like
azimidev's avatar

Anyone know how to get value of and Enum column in for instance MySQL and display it in HTML combobox?

bensampo's avatar

I was looking for a similar solution recently and ended up building an abstract enum class pulled together from lots of different resources.

I've split it out into a package in case it's of use to anyone in future: https://github.com/BenSampo/laravel-enum

As an example you can do:

final class UserType extends Enum
{
    const Administrator = 0;
    const Moderator = 1;
    const Subscriber = 2;
    const SuperAdministrator = 3;
}

And grab the value back like:

UserType::Administrator; // 0

Of course, you can then store this in the DB as an int.

As an added bonus, it's really easy to perform validation on this.

3 likes
brianthomas's avatar

@bensampo I have installed the package. It seems to be pretty handy but It some how lacks details on how i can get the enum value from API (ajax) / http request. I'm getting the below error : { "success": false, "message": "Please correct the following errors", "errors": { "customer_type": [ "The value you have entered is invalid." ] } }

Dario's avatar

@bensampo @NetoPVH I prefer the interface approach instead of an abstract class since you can extend only one class but you can implement whatever interface you require. so as all the models already extend the Eloquent Model class, I used to create an interface with all my enum constants and implementing that from my main Model class. So to speak, I have

Class ContractModel extends Model implements EnumContract
{
    ....
}

Class Post extends ContractModel
{
    public function foo()
    {
        return self::enumXXX;
    }
}

Please or to participate in this conversation.