Laravel 迁移更改列的默认值 [英] Laravel migrations change default value of column

查看:29
本文介绍了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 命令.

Then run migrate command.

更新

对于 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(); 子句.

Inside up() method instead of Schema::table(); clause.

这篇关于Laravel 迁移更改列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆