将现有列更改为在迁移中具有默认值

时间:2021-06-16 23:56:08

标签: laravel

我有一列文本,我需要让它包含一个默认值,应该如何使用迁移进行更改?

迁移创建表

<?php

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

class CreateExperienceSettingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('experience_settings', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('experience_id');
            $table->boolean('schedule_required');
            $table->boolean('voucher_print');
            $table->boolean('address_required');
            $table->boolean('automatic_refund');
            $table->boolean('show_partner_discount');
            $table->boolean('branch_required')->default(false);
            $table->text('email_validation_user')->nullable();
            $table->text('email_validation_partner')->nullable();
            $table->timestamps();

            $table->foreign('experience_id')->references('id')->on('experiences')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('experience_settings');
    }
}

列中的迁移更新更改值

<?php

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

class ChangeEmailValidationUserDefaultValue extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('experience_settings', function (Blueprint $table) {
            $table->text('email_validation_user')->default('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

但是在进行 php artisan migrate 并插入迁移时,不会在列 mysql 中插入具有默认值的数据。

1 个答案:

答案 0 :(得分:1)

仅添加 librery 学说/dbal 并运行迁移

composer require doctrine/dbal

这应该对你有用。

相关问题