有一個能夠保存每個用戶感興趣的「類別」和/或「品牌」的ID的「興趣」。 爲了防止創建兩個單獨的表對於user_category_interests
和user_brand_interest
我添加了一個名爲type
的枚舉列。參考兩個表時的Laravel DB架構
現在我不知道如何在架構生成器中創建遷移,如何設置關係和外鍵......以利用使用Eloquent方法。
Schema::create('user_interests', function (Blueprint $table) {
$table->increments('id');
$table->enum('type', ['categories', 'brands']);
$table->integer('reference_id')->unsigned();
$table->timestamps();
});
有沒有更好的方法,而不是創建兩個單獨的表和模型?
P.S.我使用laravel 5.4
Brands Table:
id | name
-----------
1 | Adidas
2 | Nike
3 | Puma
Categories Table:
id | name
-----------
1 | Clothes
2 | Luxury
3 | Sport Wear
User_Interests Table:
id | user_id | type | reference_id
-----------------------------------------
1 | 113 | 'brand' | 2
1 | 113 | 'brand' | 3
2 | 113 |'category'| 3
3 | 224 | 'brand' | 1
[Laravel可能重複,我需要多個數據庫表在不同模型上的投票](http://stackoverflow.com/questions/29222441/laravel-do-i-need-multiple-database-tables-for-票據不同模型) – patricus