簡體   English   中英

如何在Laravel 5.2中使唯一鍵不可為空?

[英]how to make unique key not nullable in Laravel 5.2?

我已經嘗試過這些是使唯一鍵不可為空的語法。

1 : $table -> string('email_id' , 40) -> unique() ;
    $table -> string('email_id') -> nullable(false) -> change();


2 : $table -> string('email_id' , 40) -> unique() -> nullable(false) -> change();

但是他們都不起作用。 如果能找到與此相關的任何幫助,那將是很好的。

先感謝您。

如果在創建表/列時找不到解決方案,則必須使用訪問器和變異器在PHP中解決此問題

例如

$table -> string('email_id' , 40) -> unique() ; //email_id is unique


//now how to make not null
public function setEmailIdAttribute($value) {
    if ($value == null) ) { // will check for null 
    //do something
    } else {
        $this->attributes['email_id'] = $value;
    }
}

如需更多幫助,請檢查

希望對您有幫助。

我已經使用$table->string('email')->unique(); 它對我有用。我認為您犯了另一個錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM