Laravel迁移更改列的默认值 [英] Laravel migrations change default value of column
本文介绍了Laravel迁移更改列的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个已经分配了默认值的表.例如,我们可以查看以下内容:
I have a table with a default value already assigned. For an example we can look at the following:
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('active')->default(1);
});
我现在想在活动字段上更改默认值.我期望做这样的事情:
I now want to change my default value on the active field. I am expecting to do something like this:
if (Schema::hasTable('users')) {
Schema::table('users', function (Blueprint $table) {
if (Schema::hasColumn('users', 'active')) {
$table->integer('active')->default(0);
}
});
}
但是,它当然告诉我该列已经存在.如何仅更新列x的默认值而不删除该列?
But of course it tells me the column is already there. How can I simply update the default value of column x without dropping the column?
推荐答案
您可以使用 change()
方法:
You can use change()
method:
Schema::table('users', function ($table) {
$table->integer('active')->default(0)->change();
});
然后运行 migrate
命令.
更新
对于Laravel 4,请使用以下代码:
For Laravel 4 use something like this:
DB::statement('ALTER TABLE `users` CHANGE COLUMN `active` `active` INTEGER NOT NULL DEFAULT 0;');
内部 up()
方法而不是 Schema :: table();
子句.
这篇关于Laravel迁移更改列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文