[英]How can I create unique index, that requires a set of attributes to be unique?
你好堆棧溢出
我想出了 Laravel 的一個問題,我不知道如何解決它。
情況如下:
我有一個遷移,名稱為“地址”:
Schema::create('adresses', function (Blueprint $table) {
$table->id();
$table->string('adress'); //adress
$table->integer('houseNumber'); //house number
$table->string('houseNumberAddition')->nullable(); //house number addition
$table->timestamps();
});
我想實現的目標:我希望地址、houseNumber 和 houseNumberAddition 一起成為 1 個唯一值。
通常您會添加 unique() 函數,以使特定字段唯一。 但我希望將地址、houseNumber 和 houseNumberAddition 組合在一起,成為一個唯一值。
(因為 houseNumber 和 houseNumberAddition 將被多次使用,而與 adress、houseNumber 和 houseNumberAddition 的組合在我的數據庫中只會存在一次。)
我希望有人有一些奇特的技巧來解決這個問題。
感謝您的幫助!
戴夫
您可以將 3 列組合在一個唯一索引中
$table->unique(['adress', 'houseNumber', 'houseNumberAddition']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.