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?

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 values ENUM('on','off','unknown')");
}

public function down()
{
    DB::statement("ALTER TABLE user_status MODIFY COLUMN values 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.


I did it with MySql:

class ChangeJobTypeEnum extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::statement("ALTER TABLE _TABLENAME_ CHANGE _COLUMNNAME_ _COLUMNNAME_ ENUM('on', 'off', 'auto')");

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement("ALTER TABLE _TABLENAME_ CHANGE _COLUMNNAME_ _COLUMNNAME_ ENUM('on', 'off')");

    }
}

I had a slightly different situation, it was necessary to add new items, change existing and remove old. This is my example.

 <?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

class ChangeEnum extends Migration
{   
    public function up()
    {
        Schema::table('table_example', function (Blueprint $table) {
            DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third', 'fourth', 'fifth', 'sixth') NOT NULL;");
            DB::statement("UPDATE `field` set `status` = 'fourth' where `status` = 'first';");
            DB::statement("UPDATE `field` set `status` = 'fifth' where `status` = 'second';");
            DB::statement("ALTER TABLE table_example MODIFY status enum('third', 'fourth', 'fifth', 'sixth') NOT NULL;");
        });
    }

    public function down()
    {
        Schema::table('table_example', function (Blueprint $table) {
            DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third', 'fourth', 'fifth', 'sixth') NOT NULL;");
            DB::statement("UPDATE `field` set `status` = 'first' where `status` = 'fourth';");
            DB::statement("UPDATE `field` set `status` = 'second' where `status` = 'fifth';");
            DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third',) NOT NULL;");
        });
    }
}

By the way, generate row SQL query via JetBrains ide(DataGrip), is like that:

 ∧_∧ 
(。・ω・。)つ━☆・*。
⊂   ノ    ・゜+.
 しーJ   °。+ *´¨)

The second answer works, but in my case CHANGE was throwing an error. So i tried using MODIFY instead and that worked. Thanks guys..

Here is my code:

class ChangeJobTypeEnum extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    DB::statement("ALTER TABLE _TABLENAME_ MODIFY _COLUMNNAME_ ENUM('on', 'off', 'auto')");

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::statement("ALTER TABLE _TABLENAME_ MODIFY_COLUMNNAME_ ENUM('on', 'off')");

}
}

Need Your Help

Convert a double array to a float array

java arrays floating-point double

I have a double[][] array, and I want to get one row into a float[] array. Casting didn't worked at first, so I looked for something different.

Enumerating through an object's properties (string) in C#

c# .net linq properties

Let's say I have many objects and they have many string properties.