How to change nullable columns to having default values in Laravel?

308 Views Asked by At

I want to change some table columns in Laravel from nullable to having a default value. I installed doctrine/dbal, and created a new migration with the following columns I want to change (previously nullable):

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

However this doesn't seem to have done anything. Is it possible to do? Thanks!

1

There are 1 best solutions below

0
STA On

You need to create a new migration using command:

php artisan make:migration update_movies_table

Then, in that created migration class, add this line, using the change method like this :

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();
        });
    }

To make these changes and run the migration, use the command:

php artisan migrate