Laravel Schema Builder 更新默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19319590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:32:52  来源:igfitidea点击:

Laravel Schema Builder Update Default Value

laravelmigrationdatabase-schemaeloquent

提问by kilrizzy

Trying to setup a migration that will make my already existing "active" field have a default value of "1".

尝试设置一个迁移,使我已经存在的“活动”字段具有默认值“1”。

I see in the docs I can use something like:

我在文档中看到我可以使用以下内容:

$table->integer('active')->default(1);

But I tried this in my migration with no success, I guess because the field already exists. Is there a way to correctly manage existing fields using the schema builder?

但是我在迁移中尝试了这个但没有成功,我猜是因为该领域已经存在。有没有办法使用模式构建器正确管理现有字段?

My current migration:

我目前的迁移:

public function up()
{
    Schema::table('scores', function($table){
        $table->integer('active')->default(1);
    });
}

Edit:

编辑:

From what I've read so far, this can't be done with the query builder. But when I try to run a raw query:

从我到目前为止所读到的内容来看,查询构建器无法做到这一点。但是当我尝试运行原始查询时:

DB::query("ALTER TABLE `scores` CHANGE COLUMN `active` `active` int(11) NOT NULL DEFAULT '1';");

I'm getting a "method 'query' does not exist error", so I'm guessing this method name was changed I just can't find what it was changed to

我收到“方法'查询'不存在错误”,所以我猜这个方法名称已更改我只是找不到它更改为的内容

回答by kilrizzy

Looks like DB::query() was changed to DB::statement()

看起来 DB::query() 已更改为 DB::statement()

This did the trick:

这做到了:

DB::statement("ALTER TABLE `scores` CHANGE COLUMN `active` `active` int(11) NOT NULL DEFAULT '1';");