【问题标题】:How to change nullable columns to having default values in Laravel?如何在 Laravel 中将可空列更改为具有默认值?
【发布时间】:2020-12-09 00:21:01
【问题描述】:

我想将 Laravel 中的一些表列从可为空更改为具有默认值。我安装了学说/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();
        });
    }

然而,这似乎并没有做任何事情。有可能吗?谢谢!

【问题讨论】:

    标签: laravel migration nullable default-value doctrine-dbal


    【解决方案1】:

    您需要使用命令创建一个新的迁移:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2022-12-04
      • 2021-06-26
      • 2021-09-01
      • 1970-01-01
      • 2016-08-28
      • 2020-02-28
      相关资源
      最近更新 更多