我有一个已分配默认值的表。例如,我们可以看看以下内容:
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('active')->default(1);
});
我现在想更改活动字段上的默认值。我期待做这样的事情:
if (Schema::hasTable('users')) {
Schema::table('users', function (Blueprint $table) {
if (Schema::hasColumn('users', 'active')) {
$table->integer('active')->default(0);
}
});
}
但它当然告诉我该专栏已经存在。如何简单地更新列 x 的默认值而不删除该列?
您可以使用change() https://laravel.com/docs/5.1/migrations#modifying-columns method:
Schema::table('users', function ($table) {
$table->integer('active')->default(0)->change();
});
然后运行migrate
命令。
Update
对于 Laravel 4,使用类似这样的内容:
DB::statement('ALTER TABLE `users` CHANGE COLUMN `active` `active` INTEGER NOT NULL DEFAULT 0;');
Inside up()
方法而不是Schema::table();
clause.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)