Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 582
Abhay Maurya Avatar asked Dec 03 '22 23:12

Abhay Maurya


2 Answers

You can do

   Schema::create('ex', function (Blueprint $table) {
        $table->integer('some_field')->unsigned()->nullable();
   });
like image 116
Antonio Carlos Ribeiro Avatar answered Dec 11 '22 10:12

Antonio Carlos Ribeiro


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").

like image 31
shalvah Avatar answered Dec 11 '22 11:12

shalvah