Make column not nullable in a Laravel migration

半腔热情 提交于 2019-11-29 20:54:42
TLGreg

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->...->nullable(false)->change();

As of Laravel 5, it's possible to reverse this natively - simply pass false as an argument to nullable().

e.g.

$table -> string('foo') -> nullable(false) -> change();
funerr

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

public function down()
{
    Schema::table('table_name', function ($table) {
        $table->dropColumn('column');
    });
}
Sridhar

the below information is for SQL
Use the below code first

UPDATE [Table] SET [Column]=0 WHERE [Column] IS NULL

than run this code

ALTER TABLE [Table] MODIFY [Column] INTEGER NOT NULL
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!