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.
Asked
Active
Viewed 1.4e+01k times
199

Francesco - FL
- 603
- 1
- 4
- 25

bilalq
- 7,279
- 3
- 22
- 28
-
1the most complete answer for this question can be found here: https://stackoverflow.com/a/32568625/4908847 – szaman Jun 07 '17 at 08:05
5 Answers
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
-
1That'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
-
3Taylor 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
-
1Actually 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
-
5See @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
-
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
-
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
-
It works! So this has become the correct answer because of updates to Laravel. – jlbang Jan 26 '16 at 21:53
-
Thanks, although I don't understand why columns aren't made non-nullable by default. It's usually best practice and this adds lots of noise to the code. – Morgan Jul 27 '18 at 17:37
-
2Columns are non-null by default. The poster was just asking how to reverse an already null column. – Matt McDonald Jul 28 '18 at 19:23
-
2
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
-
11Is 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();
});
}

Gabriel Fernandez
- 365
- 4
- 7
-
thanks, it works for me. my case was inverse. I want to make the column not nullable in the down function. – Muhammad Nauman Yousaf Jun 09 '22 at 08:07
1
In laravel 8, you just have to put this: "->nullable()"
$table->string('name')->nullable();

Sylar
- 187
- 13