Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add default value to enum type field in schema builder

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.

like image 493
Peter Avatar asked Feb 16 '15 17:02

Peter


3 Answers

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.

like image 182
Joel Hinz Avatar answered Sep 23 '22 22:09

Joel Hinz


use this :

$table->enum('status',['new', 'active', 'disabled'])->default('active');
like image 32
Almaida Jody Avatar answered Sep 20 '22 22:09

Almaida Jody


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.

like image 43
Hyder B. Avatar answered Sep 21 '22 22:09

Hyder B.