[英]Products with attributes (variants) database design?
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 : 由于Laravel不支持多列主键,因此我希望
attribute_product
表具有主键,当我调用$product->attribute->attach()
它会自动对其进行递增,因此我可以执行以下操作:
+---------+----------------------+ | 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. 在我的脑海中,我将为
attribute_product
表创建一个模型,并将其命名为Item
,因此一个购物车可以包含多个Items,我可以像$cart->items->groupBy('product_id')
一样显示它,以便可以使用它用户购物车中的所有内容。
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 . 如果您在产品和属性之间具有
belongsToMany
关系,那是因为一个产品可能具有多个属性而不是另一个产品 。
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_product
表应该看起来像这样,只要没有令人信服的理由就可以。
+---------+----------------------+
| cart | cart_product |
+---------+----------------------+
| id | cart_id |
| user_id | product_id |
+---------+----------------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.