testCode
testCode

Reputation: 117

Change existing column to have a default value in migration

I have a column in text and I need to make it contain a default value, how should the change be made using migration?

Migration create Table

<?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');
    }
}

Migration update change value in column

<?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()
    {
        //
    }
}

But when doing php artisan migrate and inserting the migration is not inserting the data with default value in column mysql.

Upvotes: 4

Views: 1988

Answers (1)

Anwar Sarmiento
Anwar Sarmiento

Reputation: 106

Only add librery doctrine/dbal and run migration

composer require doctrine/dbal

this should work for you.

Upvotes: 2

Related Questions