There a able, called 'interests' which hold the id(s) of 'categories' and/or 'brands' that each user has interest in.
To prevent creating two separate tables for user_category_interests
and user_brand_interest
I added an enum column called type
.
Now I can't figure out how should I create a migration in Schema Builder, How to set relations and foreign keys,... to take advantage of using Eloquent methods.
Schema::create('user_interests', function (Blueprint $table) {
$table->increments('id');
$table->enum('type', ['categories', 'brands']);
$table->integer('reference_id')->unsigned();
$table->timestamps();
});
Is there any better way instead of creating two separate tables and models?
P.S. I use 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