Migrate: Is it possible to change a table to add a column and already pass some value to it (excluding the nullable () option)?

0

for example I have the student table (name, age);

Then I create a migration to add the genre (M / F)

 Schema::table('estudantes', function (Blueprint $table) {
        $table->string('genero')->nullable();
    });

But instead of using nullable (), I wanted to know if it's possible to create this column with all values as M (male), for example.

Is this possible?

    
asked by anonymous 11.01.2018 / 19:27

1 answer

2

Yes you can add a default:

Schema::table('estudantes', function (Blueprint $table) {
    $table->string('genero')->default('M');
});

In this way if you do not say anything to the contrary when inserting a new row this column will be populated with the value 'M'

DOCS

    
11.01.2018 / 19:30