如何在Laravel中将可为空的列更改为具有默认值?

问题描述

我想将Laravel中的某些表列从可为空更改为具有认值。我安装了doctrine / dbal,并使用以下要更改的列(以前可以为空)创建了新迁移:

public function up()
    {
        Schema::table('movies',function (Blueprint $table) {
            $table->string('movieDirector')->default('')->change();
            $table->string('movieGenre')->default('')->change();
            $table->string('movieCast')->default('')->change();
        });
    }

但是,这似乎没有做任何事情。有可能吗?谢谢!

解决方法

您需要使用以下命令创建新的迁移:

php artisan make:migration update_movies_table

然后,在创建的迁移类中,使用change这样的方法添加以下行:

public function up()
    {
        Schema::table('movies',function (Blueprint $table) {
            $table->string('movieDirector')->default('test')->change();
            $table->string('movieGenre')->default('test')->change();
            $table->string('movieCast')->default('test')->change();
        });
    }

要进行这些更改并运行迁移,请使用以下命令:

php artisan migrate