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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With