Skip to content
Advertisement

Laravel – unsigned with nullable

I am using Laravel 5.3, I want to define a field in table migration as nullable as well as unsigned. As both are index modifier, can I use them in concatenation ? Like:

$table->integer('some_field')->unsigned()->nullable();

Please also give some reference if there is to these kind of modifications in laravel documentation or somewhere.

Please note that I want to define field in up() function as unsigned as well as nullable. I dont want solutions with down() function like:

public function up()
    {
       Schema::create('ex', function (Blueprint $table) {
            $table->integer('some_field')->unsigned();
       });
    }
public function down()
    {
        DB::statement('ALTER TABLE ex MODIFY `some_field` integer NOT NULL;');
    }

Thanks in advance!

Advertisement

Answer

You can. Laravel allows a lot of method-chaining. Each of the column methods is defined to return the same Blueprint object, so you can easily call another method on it. This means you could even do:

Schema::create('ex', function (Blueprint $table) { 
  $table->integer('some_field')->unsigned()->default(10); 
});

And all would be well 🙂

For further information, see the documentation on database migrations (see the section on “Column Modifiers”).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement