如何设置表字段在Laravel 5.4迁移中不为空
答
,如果你没有在您的迁移添加可空()方法时会自动不为空。
$table->string('col_test1')->nullable();
//This can be null. It will run a mysql statement like this
col_test1 VARCHAR(255)
$table->string('col_test2');
//This should not be null . It will run a mysql statement like this
col_test2 VARCHAR(255) NOT NULL,
如果您想了解更多详细信息,只是导航here
答
数据库字段只能是null
或not null
。
因此对于laravel,如果您在迁移中调用->nullable()
,则允许该字段为null
,否则为not null
,无需任何特定配置。
例如
// this will be not null
$table->string('col1');
// this can be null
$table->string('col1')->nullable();
'不null'是每场不得到'nullable'选项。所以如果你想让你的字段为'Not null',那么移除' - > nullable()'调用。 – DestinatioN
所以它是默认的非空,如果我不使用可为空? – jonm
是的,一个字段只能是'null'或'not null',而' - > nullable()'允许该字段为空。所以如果没有它,这个字段就是'not null' – DestinatioN