php - 更新现有的列属性 - Laravel 5 迁移

标签 php laravel laravel-5 database-migration

我有
一个名为 cpe_mac 的现有列.我通过这样的迁移创建了它:

$table->string('cpe_mac')->default(NULL)->nullable();

我想要
我想加这个->unique()到该列,而不必 放下并重新添加 .

我试过了
$table->string('cpe_mac')->unique();

迁移文件
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterCaptivePortalTable212017 extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('captive_portals', function (Blueprint $table) {
            $table->string('cpe_mac')->unique();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('captive_portals', function (Blueprint $table) {
            $table->string('cpe_mac')->default(NULL)->nullable();
        });
    }
}

我一直
得到
SQLSTATE[42701]: Duplicate column: 7 ERROR:  column "cpe_mac" of relation "captive_portals" already exists
有没有办法在不必删除现有列的情况下实现这一目标?我有很多无法删除的客户端数据。

最佳答案

Schema::table('users', function (Blueprint $table) { $table->string('cpe_mac')->unique()->change(); });
https://laravel.com/docs/5.0/schema#changing-columns

关于php - 更新现有的列属性 - Laravel 5 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41987609/

相关文章:

php - 如何在 Laravel 5.4 中验证文件名

laravel-5 - Laravel Backpack 如何在没有 CRUD 的情况下创建新页面

PHP 安全 - (int) 与 FILTER_VALIDATE_INT

php - 将 Codeception 与表单输入数组一起使用

git - 如何使用 Git ignore file 来防止将 vendor 文件夹和其他 laravel 的配置文件上传到 git 分支。什么是正确的方法?

php - 如何从复选框中的表中读取数据

php - 我可以在 PHP 中拦截用户定义的全局函数吗?

php - 在 PHP 中检查日期是否早于 12 小时

mysql - 内存中的 Laravel 5 测试

php - 如何将laravel迁移文件迁移到mongodb