Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter ENUM column and add value to that column in Laravel

I have a MySQL database with a table called user_level_attempt. That table has a ENUM type column with ['PROGRESSED', 'STOPPED', 'COMPLETED'] values. I need to write a migration to add another value (let's say 'PASSED') to that column. After adding, it'll look like this, ['PROGRESSED', 'STOPPED', 'COMPLETED', 'PASSED]. How can I do that in Laravel? I tried the following solution but it doesn't seem like a good practice/solution.

 /**
         * Schema table name to migrate
         * @var string
         */
        public $set_schema_table = 'bt_user_level_attempt';


        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table($this->set_schema_table, function ($table) {
                $table->dropColumn('status');
            });

            Schema::table($this->set_schema_table, function ($table) {
                $table->enum('status', ['PROGRESS', 'STOPPED', 'COMPLETED', 'PASSED'])->default('PROGRESS')->after('effective_time_spend');
            });
        }

/**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table($this->set_schema_table, function ($table) {
            $table->dropColumn('status');
        });

        Schema::table($this->set_schema_table, function ($table) {
            $table->enum('status', ['PROGRESS', 'STOPPED', 'COMPLETED'])->default('PROGRESS')->after('effective_time_spend');
        });
    }

Thank you.

like image 605
Nadun Malinda Avatar asked Feb 21 '19 12:02

Nadun Malinda


People also ask

How do I add a column in laravel without losing data?

As per the docs, you just need to create a separate migration to create the new column. This will allow you to add a column without resetting or rolling back your tables, and thus prevent you from losing your data.

How do I change columns in laravel?

Renaming Columns To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer. json file: Schema::table('users', function (Blueprint $table) { $table->renameColumn('from', 'to'); });

Can enum be nullable laravel?

Enums can't be nullable.


2 Answers

After all, I figure out to find a solution. Thanks to all fellows for enlightening me. :)

/**
     * Schema table name to migrate
     * @var string
     */
    public $set_schema_table = 'bt_user_level_attempt';


    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED', 'PASSED') NOT NULL DEFAULT 'PROGRESS'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement("ALTER TABLE ".$this->set_schema_table." MODIFY COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED') NOT NULL DEFAULT 'PROGRESS'");
    }
like image 156
Nadun Malinda Avatar answered Sep 18 '22 20:09

Nadun Malinda


You should try with DB::statement method:

Use the DB::statement method:

DB::statement("ALTER TABLE ".$this->set_schema_table." CHANGE COLUMN status ENUM('PROGRESS', 'STOPPED', 'COMPLETED','PASSED') NOT NULL DEFAULT 'PROGRESS'");
like image 27
AddWeb Solution Pvt Ltd Avatar answered Sep 20 '22 20:09

AddWeb Solution Pvt Ltd