ChatGPT解决这个技术问题 Extra ChatGPT

Make column not nullable in a Laravel migration

I'm writing a migration to make certain columns in a table nullable right now. For the down function, I of course want to make those columns not nullable again. I looked through the schema builder docs, but couldn't see a way to do this.

Any help would be appreciated.

the most complete answer for this question can be found here: stackoverflow.com/a/32568625/4908847

D
Daniel Dewhurst

Prior to Laravel 5, there was no Laravel native way of altering an existing table column using the schema builder. You'd need to use raw queries for this.

However, as of Laravel 5 you can use:

$table->string('foo')->nullable(false)->change();

You must have the dbal dependency prior to running the above command:

composer require doctrine/dbal

That's what I found as well. A nice touch would for the schema builder to allow for altering column definitions, which it does not seem to support. I'm sure there are many others like myself who are using the schema builder to modify an existing DB, not just creating tables from scratch.
Taylor Otwell (creator of Laravel) said 6 days ago (2014-05-09): "I still stand by my statement that if anyone can successfully and cleanly do it I will merge it." github.com/laravel/framework/issues/895#issuecomment-42709756
@Musa Apparently you can set a column nullable (ex: $table->string('colmn', 255)->nullable()->change();) but the reverse doesn't appear to work ($table->string('colmn', 255)->change();), so you still need to use raw db queries for this
See @MattMcDonald's answer below. You can use nullable() to make it nullable and nullable(false) to make it Not nullable in a migration.
nullable(false) doesn't work for me in Laravel 5.3 :(
M
Matt McDonald

As of Laravel 5 it's possible to reverse this by passing false as an argument to nullable.

$table->string('foo')->nullable(false)->change();

It works! So this has become the correct answer because of updates to Laravel.
Thanks, although I don't understand why columns aren't made non-nullable by default. It's usually best practice and this adds lots of noise to the code.
Columns are non-null by default. The poster was just asking how to reverse an already null column.
Required first composer require doctrine/dbal
M
Matt McDonald

First run this:

composer require doctrine/dbal

Then create a migration that will alter the table like so:

php artisan make:migration fix_whatever_table_name_here

public function up()
{
    Schema::table('table_name', function (Blueprint $table) {
        $table->type('column')->nullable(false)->change();
    });
}

# Optional:
# public function down()
# {
#     Schema::table('table_name', function ($table) {
#         $table->type('column')->nullable()->change();
#     });
# }

Is there a reason to drop the entire column in the rollback routine? The down() method should just undo the up() method's logic to support rolling migrations backward and forward.
G
Gabriel Fernandez

You can just declare the column again without ->nullable() and use ->change

public function up()
{
    Schema::table('table_name', function (Blueprint $table) {
        $table->type('column')->nullable(false)->change();
    });
}

public function down()
{
    Schema::table('table_name', function ($table) {
        $table->type('column')->nullable()->change();
    });
}

thanks, it works for me. my case was inverse. I want to make the column not nullable in the down function.
S
Sylar

In laravel 8, you just have to put this: "->nullable()"

$table->string('name')->nullable();