laravel 将默认值添加到架构构建器中的枚举类型字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28547038/
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
Add default value to enum type field in schema builder
提问by Peter
I'm using the following method to create a database column of type ENUM
in schema builder:
我正在使用以下方法ENUM
在模式构建器中创建类型为数据库的列:
$table->enum('status', array('new', 'active', 'disabled'));
I'd like to set it's default value to active
.
I tried to do this:
我想将它的默认值设置为active
.
我试图这样做:
$table->enum('status', array('new', 'active', 'disabled'))->default('active');
But as you can guess it doesn't set it's default value. I'm using a MySQL database if that's important.
但是你可以猜到它没有设置它的默认值。如果这很重要,我正在使用 MySQL 数据库。
回答by Joel Hinz
From the MySQL manual:
从MySQL 手册:
If an ENUM column is declared to permit NULL, the NULL value is a legal value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
如果 ENUM 列声明为允许 NULL,则 NULL 值是该列的合法值,默认值为 NULL。如果 ENUM 列声明为 NOT NULL,则其默认值是允许值列表的第一个元素。
I'm assuming this means you should set 'active'
as the first value, remove the default()
call, and possibly set NULL
permittance manually.
我假设这意味着您应该设置'active'
为第一个值,删除default()
调用,并可能NULL
手动设置许可。
回答by Almaida Jody
use this :
用这个 :
$table->enum('status',['new', 'active', 'disabled'])->default('active');
回答by Hyder B.
I ran into a similar issue, that's what worked for me:
我遇到了类似的问题,这对我有用:
$table->enum('status', array('active', 'new', 'disabled'));
Place the default value as the first element in the array. active
is now the default value.
将默认值作为数组中的第一个元素。active
现在是默认值。