199

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.

Francesco - FL
  • 603
  • 1
  • 4
  • 25
bilalq
  • 7,279
  • 3
  • 22
  • 28
  • 1
    the most complete answer for this question can be found here: https://stackoverflow.com/a/32568625/4908847 – szaman Jun 07 '17 at 08:05

5 Answers5

336

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
Daniel Dewhurst
  • 2,533
  • 2
  • 21
  • 39
TLGreg
  • 8,321
  • 3
  • 23
  • 12
  • 1
    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. – Sean the Bean Jan 25 '13 at 19:29
  • 3
    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." https://github.com/laravel/framework/issues/895#issuecomment-42709756 – Ryan May 15 '14 at 21:30
  • 1
    Actually now you can do it in laravel 5: http://laravel.com/docs/5.0/schema#changing-columns – Musa Haidari May 21 '15 at 11:38
  • 3
    @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 – Luís Cruz May 21 '15 at 13:41
  • I've found a more explicit answer and some guidelines [here](http://stackoverflow.com/a/24425013/1498118) – a.barbieri Aug 11 '15 at 22:17
  • 5
    See @MattMcDonald's answer below. You can use nullable() to make it nullable and nullable(false) to make it Not nullable in a migration. – ajon Jan 11 '16 at 21:41
  • 8
    `nullable(false)` doesn't work for me in Laravel 5.3 :( – Stalinko Mar 24 '17 at 15:51
  • This works, however, I had an INT column which was unsigned. When I tried to remove nullable, it also tried to remove unsigned. OTOH, it wouldn't remove nullable when I left out `nullable()` instead of doing `nullable(false)` explicitly. I'm a bit worried about that because it seems self-contradictory. At least I got it with this `$table->integer('foo')->unsigned()->nullable(false)->change()` – aross Jan 04 '18 at 16:52
  • @aross Use `unsignedInteger('foo')` for an unsigned integer shorthand. – Matt McDonald Mar 21 '19 at 10:38
  • @MattMcDonald If I was codegolfing I would care about the 4 bytes saved, but thanks anyway :) – aross Mar 21 '19 at 13:01
  • nullable(false) doesn't work for me in Laravel 5.6 on a string column – Martijn Imhoff May 07 '19 at 11:03
  • Required first `composer require doctrine/dbal` – Eduardo Cuomo Oct 02 '20 at 19:47
53

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

$table->string('foo')->nullable(false)->change();
Matt McDonald
  • 4,791
  • 2
  • 34
  • 55
12

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();
#     });
# }
Matt McDonald
  • 4,791
  • 2
  • 34
  • 55
funerr
  • 7,212
  • 14
  • 81
  • 129
  • 11
    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. – Andrew Jul 10 '19 at 08:37
9

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();
    });
}
1

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

$table->string('name')->nullable(); 
Sylar
  • 187
  • 13