在LARAVEL中更改表的枚举列

时间:2018-06-07 14:04:38

标签: php laravel

我有一个我通过迁移创建的表,我将货币表中的一列设置为enum,它只能包含3个值。如何在symbol具有枚举属性的情况下将列symbol更改为varchar,并从列中删除$symbols变量

这是我的迁移

public function up()
{
    $symbols = ['₦', '$', '£'];
    Schema::create('currencies', function (Blueprint $table) use($symbols) {
        $table->increments('id');
        $table->string('name', 50);
        $table->string('code', 5);
        $table->enum('symbol', $symbols);
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{   
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');
    Schema::dropIfExists('currencies');
    DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}

1 个答案:

答案 0 :(得分:2)

使用以下步骤/组件创建新迁移:

  • 首先将当前符号列重命名为其他内容。
  • 然后按照您所希望的方式创建符号列,例如您所说的varchar。

    Schema::table('currencies', function (Blueprint $table) {
        $table->renameColumn('symbol','tempName'); //Rename to temp column
        $table->string('symbol'); //New symbol column
    });
    
  • 然后遍历所有现有行并获取重命名符号的临时列的值,并在新创建的符号列上设置它们。 (可以通过多种方式完成,你应该能够解决这个问题)

  • 然后删除临时列。

    Schema::table('currencies', function (Blueprint $table) {
        $table->dropColumn('tempName'); //Remove the temp column
    });
    
相关问题