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

Asked

Viewed 131 times

0

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

Then I create a Migration to add the genus (M/F)

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

but instead of using nullable(), I wanted to know if it is possible to create this column with all values like M (male), for example.

Is that possible?

  • 1

    Try $table->char("genero", 1)->default("M");

  • worked, thanks!

1 answer

2


Yes you can add a default:

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

This way if you do not say otherwise when inserting a new row this column will be populated with the value 'M'

DOCS

  • 1

    It worked! Thank you very much! =)

  • @Ygordutra nothing. You must accept the answer so, under the arrows on the left

Browser other questions tagged

You are not signed in. Login or sign up in order to post.