简体   繁体   English

Laravel 数据库设计多级分类与产品明细表

[英]Laravel database design multi-level categories with product details table

I'm trying to make relashionships between multi-level categories and subcategories, with product details table.我正在尝试使用产品详细信息表在多级类别和子类别之间建立关联。

at first i have categories table :起初我有类别表:

+----+----------+
| id | name     |
+----+----------+
| 1  | vehicles |
+----+----------+

and i have separated table for each category like category_cars table :我已经为每个类别分开了表,比如category_cars表:

+----+-----------+---------+-------+
| id | parent_id | mileage | motor |
+----+-----------+---------+-------+
| 1  | 1         | 1000    | 1600  |
+----+-----------+---------+-------+

and the products table :产品表:

+----+-------------+-------+
| id | category_id | title |
+----+-------------+-------+
| 1  | 1           | 1000  |
+----+-------------+-------+

basically the category_id in the products table is the id of category_cars基本上在产品表中的CATEGORY_IDcategory_carsid

and when i create a product i set it's title in products table, then add the other attributes of the proudct to the category_cars table like mileage , motor , ...当我创建一个产品时,我在产品表中设置了它的标题,然后将产品的其他属性添加到category_cars表中,例如mileagemotor...

so what if i need to create a sub-category of vehicles eg: cars , in this case i have no idea where to create the product.那么如果我需要创建一个子类别的车辆,例如:汽车,在这种情况下我不知道在哪里创建产品。

what i'm trying to achieve is to have multi-level categories, like 9 static parent categories and dynamically created sub-categories我想要实现的是拥有多级类别,例如 9 个静态父类别和动态创建的子类别

and a product attributes based on which product is this eg: a car, or a motorcycle.以及基于此产品的产品属性,例如:汽车或摩托车。 to be able to have separated data (attribute) for each product type.能够为每种产品类型分离数据(属性)。

there's a default columns that will be globally for each product, like name,price and each product will need to have it's extra columns depending on it's type.每个产品都有一个全局默认列,例如名称、价格,并且每个产品都需要根据其类型具有额外的列。

so the point is to get product this way :所以关键是通过这种方式获得产品:

product =>
    [
        name = myproduct
        price = 200
        category [
            id = 1
            name = vehicles
        ]
        product details [
            milage = 1000
            motor = 1600
            ...
        ]
        ....
    ]

and display all products from a category whether it's main category or sub-category.并显示类别中的所有产品,无论是主类别还是子类别。

i can't figure out how i can make a relashonship between these.我不知道如何在这些之间进行重新调整。 the logic itself is so complicated for me.逻辑本身对我来说太复杂了。

so how can i achieve this?那么我怎样才能做到这一点?

this model is named EVA这个模型被命名为EVA

entity value attribute实体值属性

i made this befor and i will share my solution with u我之前做过这个,我会和你分享我的解决方案

  • category table (id , title , parent_id)类别表(id、title、parent_id)
  • attributes table (id, title)属性表(id,标题)
  • product table (id , title , price , category_id ,.. )产品表( id , title , price , category_id ,.. )
  • product_attribute table ( product_id, attr_id , value) product_attribute 表( product_id, attr_id , value)

and if u want to make attributes based on category u will make this table also:如果你想根据类别制作属性,你也会制作这张表:

  • category_attribute (category_id , attribute_id ) category_attribute (category_id , attribute_id )

It's a mistake to make separated table for each category like category_cars table because this means each new category in the future will need new table.category_cars表那样为每个类别制作单独的表是错误的,因为这意味着将来每个新类别都需要新表。

It's more prudent to make self join in categories table and add new column called parent_id in categories table.它更谨慎,使自己在类表连接和类别表中添加新列称为PARENT_ID。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM