在LARAVEL中更改Table的枚举列 [英] Change enum column of Table in LARAVEL
问题描述
我有一个通过迁移创建的表,我将货币表中的一列设置为 enum
,它只能包含3个值.我如何在 symbol
具有枚举属性的同时将 symbol
列更改为varchar,并从该列中删除 $ symbols
变量>
这是我的迁移
公共函数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();});}/***反向迁移.** @返回无效*/公共功能down(){DB :: statement('SET FOREIGN_KEY_CHECKS = 0');Schema :: dropIfExists('currencies');DB :: statement('SET FOREIGN_KEY_CHECKS = 1');}
使用以下步骤/组件创建新迁移:
- 首先将您当前的符号列重命名为其他名称.
-
然后按照您想要的方式创建符号列,例如您所说的那样,作为varchar.
Schema :: table('currencies',function(Blueprint $ table){$ table-> renameColumn('symbol','tempName');//重命名为临时列$ table-> string('symbol');//新的符号栏});
-
然后遍历所有现有行,并获取您将符号重命名为的临时列的值,并将其设置在新创建的符号列上.(可以通过几种方法完成,您应该能够弄清楚)
-
然后删除临时列.
Schema :: table('currencies',function(Blueprint $ table){$ table-> dropColumn('tempName');//删除临时列});
I have a table which i created by migration, and I set the a column in the currency table to enum
and it can only contain 3 values. How can i change the column symbol
to varchar at the moment symbol
is of enum property and also remove the $symbols
variable from the column
This is my migration
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');
}
Create a new migration with the following steps/components:
- First rename your current symbol column to something else.
Then create the symbol column the way that you want it, for example like you said, as a varchar.
Schema::table('currencies', function (Blueprint $table) { $table->renameColumn('symbol','tempName'); //Rename to temp column $table->string('symbol'); //New symbol column });
Then loop through all existing rows and take the value of the temporary column that you renamed symbol to and set them on the newly created symbol column. (Can be done several ways, you should be able to figure that out)
Then remove the temporary column.
Schema::table('currencies', function (Blueprint $table) { $table->dropColumn('tempName'); //Remove the temp column });
这篇关于在LARAVEL中更改Table的枚举列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!