php - 在 Laravel 中更改 ENUM 列并为该列添加值
问题描述
我有一个MySQL数据库,其中有一个名为 user_level_attempt 的表。该表有一个带有['PROGRESSED', 'STOPPED', 'COMPLETED']值的ENUM类型列。我需要编写迁移以向该列添加另一个值(比如说“通过”)。添加后,它看起来像这样,['PROGRESSED', 'STOPPED', 'COMPLETED', 'PASSED]。我怎么能在 Laravel 中做到这一点?我尝试了以下解决方案,但它似乎不是一个好的做法/解决方案。
/**
* 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');
});
}
谢谢你。
解决方案
毕竟,我想找到解决办法。感谢所有给我启发的小伙伴。:)
/**
* 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'");
}
推荐阅读
- react-boilerplate - What server is react boilerplate using "under the hood"?
- css - 如何在调整浏览器大小时阻止我的文本分解,同时保持与浏览器边缘的恒定距离?
- ruby-on-rails - select2 rails 通过过滤器选项搜索?
- angular - 使用 ionic 3 和 angular 5 在我的应用程序中列出本地 mp3 文件
- jquery - 删除某些 div 元素的父 href 属性
- rest - 如何将多种安全机制应用于 Swagger 生成的 REST 服务?
- python - Kafka:如何根据时间戳消费数据
- monaco-editor - 从 Monaco 编辑器中删除 Listner
- powershell - ConEmu:启动 PowerShell 的问题
- avfoundation - 如何一次下载多个视频?