I have found this question very similar to mine Make column not nullable in a Laravel migration though it is almost 3 years old and it surely does not belong to Laravel 5
My problem is that I have a migration which in the up
function it modifies a column to make it nullable.
Now, in the down
function I want to make it not nullable again.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* THIS IS WHAT I WOULD EXPECT TO DO
THOUGH OF COURSE THE FUNCTION notNullable DOES NOT WORK */
$table->string('mycolumn')->notNullable()->change();
});
}
I could achieve that using raw SQL, but I would like to do it using Laravel methods if it is possible... but I couldn't find it, probably it hasn't been implemented it in the version 5 either.
Make sure new insertions to the table will follow to add value to the column. So change your insert queries accordingly. Create a queue to update older records and run it. After the the queue finishes, convert the field into foreign key.
$table->unique('slug'); So you add unique index to existing 'slug'.
To reset it back to not nullable. You may try this.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* By default it's NOT NULL */
$table->string('mycolumn')->nullable(false)->change(); // <--- here
});
}
By default its NOT NULL, so you should try this.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* By default it's NOT NULL */
$table->string('mycolumn')->change();
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With