[英]Add default value to enum type field in schema builder
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
. 我想将它的默认值设置为active
。
I tried to do this: 我试着这样做:
$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数据库,如果这很重要的话。
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. 如果声明ENUM列允许NULL,则NULL值是列的合法值,默认值为NULL。 If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values. 如果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
permit。
用这个 :
$table->enum('status',['new', 'active', 'disabled'])->default('active');
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
现在是默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.