2017-08-08 150 views
0

我正在使用laravel 5.4遷移。有nullable()選項。但是如何在Laravel 5.4遷移中將表字段設置爲Not null?如何設置表字段在Laravel 5.4遷移中不爲空

+0

'不null'是每場不得到'nullable'選項。所以如果你想讓你的字段爲'Not null',那麼移除' - > nullable()'調用。 – DestinatioN

+0

所以它是默認的非空,如果我不使用可爲空? – jonm

+1

是的,一個字段只能是'null'或'not null',而' - > nullable()'允許該字段爲空。所以如果沒有它,這個字段就是'not null' – DestinatioN

回答

1

,如果你沒有在您的遷移添加可空()方法時會自動不爲空。

$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

1

數據庫字段只能是nullnot null

因此對於laravel,如果您在遷移中調用->nullable(),則允許該字段爲null,否則爲not null,無需任何特定配置。

例如

// this will be not null 
$table->string('col1'); 
// this can be null 
$table->string('col1')->nullable(); 
相關問題