简体   繁体   English

许多多对多关系Laravel

[英]Many Many-To-Many relationships Laravel

I am trying to achieve the following: I am trying to set up the database for an ecommerce like website and I have a Products table. 我正在尝试实现以下目标:我正在尝试为类似网站的电子商务设置数据库,并且我有一个Products表。 Each product might have several SpecificationsHeader (which is something like the 'title' for each specificationsgroup), "under" this SpecificationsHeader could be many Specifications (those again act like 'titles', like 'Size'), for each one of those Specifications, a value should be assigned like 'XXL'. 每个产品可能有多个SpecificationHeader(每个规范组的“标题”之类的东西),在此SpecificationHeader的“下面”可能有许多规范(对于每个规范,它们的行为又像“ titles”,如“ Size”)。 ,应分配一个类似于“ XXL”的值。 The main difficulty i face is getting the relationships correct since i would like to have all of them kinda reusable. 我面临的主要困难是要正确建立关系,因为我希望所有关系都可以重用。

Example Product 1: 示例产品1:

Product : iPhone 商品名 :iPhone

SpecificationHeader : 'CPU & RAM' SpecificationHeader :'CPU和RAM'

Specification (s): 'CPU speed' - SpecificationValue : '1.4GHz' 规格 :'CPU速度'- 规格值 :'1.4GHz'

Specification (s): 'Ram size' - SpecificationValue : '2GB' 规格 :“ Ram大小” -SpecificationValue :“ 2GB”

... ...

SpecificationHeader : 'Camera' SpecificationHeader :“相机”

Specification (s): 'Camera Resolution' - SpecificationValue : '12MP' 规格 :“相机分辨率”- 规格值 :“ 12MP”


Example Product 2: (might share some of the above values or might have completely different) 示例产品2 :(可能共享上述某些值,或者可能完全不同)

Product : Samsung 商品名 :三星

SpecificationHeader : 'CPU & RAM' SpecificationHeader :'CPU和RAM'

Specification (s): 'CPU speed' - SpecificationValue : '3GHz' 规格 :'CPU速度'- 规格值 :'3GHz'

Specification (s): 'Ram size' - SpecificationValue : '4GB' 规格 :'Ram size'- 规格值 :'4GB'

Specification (s): '4G' - SpecificationValue : 'Yes' 规格 :“ 4G”- 规格值 :“是”

... ...

SpecificationHeader : 'Networking' SpecificationHeader :“网络”

Specification (s): 'Something' - SpecificationValue : 'somevalue' 规范 (S): '东西' - SpecificationValue: 'someValue中'


I experimented with several Pivot tables but i am obviously doing something wrong since the best i managed is getting the headers and specifications but not getting the values. 我尝试了几个数据透视表,但是显然我做错了,因为我管理的最好的方法是获取标题和规范,但没有获取值。 My latest approach was this: 我最新的方法是这样的: mysqldiagramm

But all of you who know what are doing will find it utterly stupid. 但是所有知道正在做什么的人都会发现它完全愚蠢。 So, the ideal solution would be : Fetching the product brings with it a specification - specificationValue pair, grouped under the specificationheader. 因此, 理想的解决方案是 :提取产品时会附带一个规范-规范值对,该规范对归入规范标题下。

Any help would be appreciated! 任何帮助,将不胜感激!

If I were you I would change only one thing, it's to set belongsTo relation between SpecificationValue and Specification 如果我是您,我只会更改一件事,那就是设置SpecificationValueSpecification之间的belongsTo关系

Like this: 像这样: 在此处输入图片说明

But it produce some limits like if you have specifications memory and RAM you wont be able to use the same specification value 8 gb for each. 但是它会产生一些限制,例如如果您有规格memoryRAM您将无法为每个规格使用相同的8 gb规格值。

And there are some positive moments. 还有一些积极的时刻。 Example: User has added new specification to product size . 示例:用户已为产品size添加了新的规格。 You will show only needed values such as s , m , l , xl etc. without values from the other specifications like 3 Gb , Plastic , 4x4 您将只显示所需的值,例如smlxl等,而没有显示其他规格的值,例如3 GbPlastic4x4

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

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