在laravel 5.2中将列添加到具有列位置和唯一约束的表中

时间:2017-11-30 10:31:21

标签: mysql laravel laravel-5.2

尝试在没有刷新和

的情况下将列添加到laravel5.2中的表

该列应具有唯一约束,并可设置该列的位置。

public function up()
{
    Schema::table('tbl_group_law_master', function($table) {
    $table->string('group_id')->unique()->after('lm_id');
 });
}

   public function down()
   {
    Schema::table('tbl_group_law_master', function($table) {
    $table->dropColumn('group_id');
   });
}

这不起作用

1 个答案:

答案 0 :(得分:0)

创建一个新的迁移文件,用于在现有表中添加新列。

新迁移将如下所示 -

public function up()
{
    Schema::table('tbl_group_law_master', function (Blueprint $table) {
        //if display_name is not added, add this column
        if(!Schema::hasColumn('tbl_group_law_master','group_id')) {
            $table->string('group_id', 20)->after('lm_id')->unique();
        }
    });
}

public function down()
{
    Schema::table('tbl_group_law_master', function (Blueprint $table) {
        //if display_name is not added, add this column
        if(Schema::hasColumn('tbl_group_law_master','group_id')) {
            $table->dropColumn('group_id');
        }
    });
}

然后运行命令 - php artisan migrate

相关问题