Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I create a migration to add a value to an enum in eloquent

I have a table that contains an enum field

CREATE TABLE `user_status` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `values` enum('on', 'off'),   PRIMARY KEY (`id`), ) ENGINE=InnoDB; 

how can I create a migration to add a value to the enum field?

like image 461
user391986 Avatar asked Feb 19 '14 05:02

user391986


People also ask

Can you add values to enum?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.


1 Answers

Laravel doesn't provide methods to update an enum column. You can delete and recreate the column but you could loose data during the operation and it's not really clean.

In this case, I think that the best choice is to write raw SQL into a migration :

public function up() {     DB::statement("ALTER TABLE user_status MODIFY COLUMN ENUM('on','off','unknown')"); }  public function down() {     DB::statement("ALTER TABLE user_status MODIFY COLUMN ENUM('on','off')"); } 

I could have made a mistake in the SQL syntax, I have never used ENUM, but you can see the idea anyway.

like image 172
Alexandre Butynski Avatar answered Oct 14 '22 10:10

Alexandre Butynski