简体   繁体   中英

Laravel 5: Add new value in my column migration

I have existing table and columns. One of my column named status the datatype is enum('0','1') with the default value of 0. And right now I want to add more values in my status column. Will look like this enum('0','1','2','3') with also default value of 0.

My Migration

public function up()
    {
        //
         DB::statement("ALTER TABLE purchase_requisitions CHANGE status ENUM('0', '1', '2','3','4','5')")->default('0')->comment('0 = Unproccessed 1 = Processed');
    }

When I migrate this there's an error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
  n your SQL syntax; check the manual that corresponds to your MySQL server v
  ersion for the right syntax to use near '('0', '1', '2','3','4','5')' at li
  ne 1

UPDATE:

Schema::table('purchase_requisitions', function (Blueprint $table) {
            $table->enum('status')->default('0')->comment('0 = Unproccessed 1 = Processed')->change();
        });

Question: How can I implement this using the migration?

There are two issues here. Firstly, your SQL to change the enum values is incorrect. It should be:

ALTER TABLE purchase_requisitions MODIFY COLUMN status ENUM('0', '1', '2','3','4','5')

Secondly, you can't chain method onto the output of DB::statement , only on to column declarations. If the comment/default is the same as before, you can just leave the update out. If not, you can do something like:

DB::statement("ALTER TABLE purchase_requisitions MODIFY COLUMN status ENUM('0', '1', '2','3','4','5') DEFAULT '0' COMMENT '0 = Unproccessed 1 = Processed'"); 

With the help of @atymic, there's some error in his code but I managed to fixed it. It should be like this.

Schema::table('purchase_requisitions', function (Blueprint $table) {
        DB::statement("ALTER TABLE purchase_requisitions MODIFY COLUMN status ENUM('0', '1', '2','3','4','5') DEFAULT '0'  COMMENT '0 = Unproccessed 1 = Processed'");
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM