Hello I have two tables with many to many relationship and a junction table. In short, products with different attributes which have added price with them. to make it more clear I It's product and attribute table.
+-------------+-----------+-------------------+ | product | attribute | attribute_product | +-------------+-----------+-------------------+ | id | id | product_id | | name | name | attribute_id | | description | | attribute_value | | price | | added_price | +-------------+-----------+-------------------+
as Laravel doesn't support multi column primary key I want the attribute_product
table to have a primary key and when I call $product->attribute->attach()
it automatically increment it, so I can do the following :
+---------+----------------------+ | cart | cart_product | +---------+----------------------+ | id | cart_id | | user_id | attribute_product_id | +---------+----------------------+
In my head I'm gonna make a model for attribute_product
table and call it Item
and therefore a cart can have multiple Items, and I can show it like $cart->items->groupBy('product_id')
so I can have it all there in the user's cart.
Any solution or a better suggestion is very much appreciated.
Well, I think that maybe you aren't focusing it in the right way. If you have a belongsToMany
relationship between product and attribute , is because a product may have multiple attributes without being a different product .
In your cart you should add a product, not an specific attribute of a product. For example:
I want a water bottle, but only the water, without the bottle.
This makes no sense, you can not buy a water bottle without the bottle. In that case you will be buying water, not a water bottle, which should be different products, not the same one.
The cart_product
table should look like this, as long as there is no compelling reason why it can not be so.
+---------+----------------------+
| cart | cart_product |
+---------+----------------------+
| id | cart_id |
| user_id | product_id |
+---------+----------------------+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.