13

I'm using the following method to create a database column of type ENUM in schema builder:

$table->enum('status', array('new', 'active', 'disabled'));

I'd like to set it's default value to active.
I tried to do this:

$table->enum('status', array('new', 'active', 'disabled'))->default('active');

But as you can guess it doesn't set it's default value. I'm using a MySQL database if that's important.

Peter
  • 233
  • 2
  • 4
  • 8

3 Answers3

17

From the MySQL manual:

If an ENUM column is declared to permit NULL, the NULL value is a legal value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.

I'm assuming this means you should set 'active' as the first value, remove the default() call, and possibly set NULL permittance manually.

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
  • Thanks, should have checked the manual first, thought it was a Laravel thing. Will accept as soon as the time limit lets me. – Peter Feb 16 '15 at 17:40
  • You mean remove `NULL` permission, isntead of setting it, right? – Adam Feb 28 '18 at 09:43
  • The answer is over three years old @Adam, but I probably meant that you might have to explicitly set whether to allow null or not. – Joel Hinz Feb 28 '18 at 11:49
15

use this :

$table->enum('status',['new', 'active', 'disabled'])->default('active');
Almaida Jody
  • 558
  • 2
  • 9
  • 19
4

I ran into a similar issue, that's what worked for me:

$table->enum('status', array('active', 'new', 'disabled'));

Place the default value as the first element in the array. active is now the default value.

Hyder B.
  • 10,900
  • 5
  • 51
  • 60