使用 Laravel 迁移将默认值设置为 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27558713/
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-08 16:49:56  来源:igfitidea点击:

Set default to NULL with laravel migration

laravel

提问by James Stott

I am adding a field to a table in a migration that I wish to allow to be NULL but also I wish for it to default to NULL. What do I place in the default method? I fear that putting "NULL"in will attempt to place a string of NULLin which I obviously don't want. Please help :)

我正在向迁移中的表添加一个字段,我希望允许为 NULL,但也希望它默认为 NULL。我在默认方法中放置什么?我担心放入"NULL"会尝试放置一串NULL我显然不想要的字符串。请帮忙 :)

Schema::table('item_categories', function(Blueprint $table)
{
    $table->integer('parent_item_category_id')->unsigned()->nullable()->default($what_to_put here);
});

回答by James Stott

When you use the nullable()method on a field, that field will default to NULL.

当您nullable()在字段上使用该方法时,该字段将默认为 NULL。