简体   繁体   English

雄辩的ORM | 试图了解Laravel 5.6中的关系

[英]Eloquent ORM | Trying to understand relations in Laravel 5.6

Stackoverflow . Stackoverflow

I'm newbie to PHP and Laravel. 我是PHP和Laravel的新手。 I'm learning it by creating ecommerce shop system . 我正在通过创建电子商务商店系统来学习它。 So the question is about Eloquent ORM and Relations in system. 因此,问题在于系统中雄辩的ORM和关系。

I have Brands Catalog with Products that belongs to Brand. 我有品牌目录以及属于品牌的产品。 Than I want to create Catalog with Products, Types, Variants and Photos. 比我想用产品,类型,变体和照片创建目录。


Project Scheme 项目计划

On scheme below you can see the relations and DB migrations done now. 在下面的方案中,您可以看到关系和数据库迁移现在已完成。

方案

The Problem 问题

What I have done: 我做了什么:

  • Models relations; 模型关系;
  • Brand controller (simple CRUD with slug); 品牌控制者(带子弹的简单CRUD);

What I need to do: 我需要做什么:

  1. Create adding Product logic (Only 1 ProductVariant). 创建添加产品逻辑(仅1个ProductVariant)。

    When adding new Product, by default, it should create 1 ProductVariant with datas (eg ASUS (Brand), ROG (Product), Notebook (Type), Zephyrus (Variant)). 添加新产品时,默认情况下,它应使用数据创建1个ProductVariant(例如ASUS(品牌),ROG(产品),Notebook(类型),Zephyrus(Variant))。

  2. Create adding Product logic (More than 1 ProductVariant). 创建添加产品逻辑(超过1个ProductVariant)。

    When adding new Product, but Product has >1 ProductVariant(eg Intel (Brand), Core i7 (Product), CPU (Type) and Variants 7700, 7700K, 7600). 添加新产品时,产品具有> 1个ProductVariant(例如Intel(品牌),Core i7(产品),CPU(类型)和变体7700、7700K,7600)。

So, when I'm creating product (eg product/add.blade.php view). 因此,当我创建产品时(例如product / add.blade.php视图)。 With example form: 带有示例表格:

<form action="{{ route('name of add route) }} method="post">

    here goes brand select with options (foreach brands as brand)

    than goes type select with options (foreach types as type)

    than goes inputs for Product (name, country, etc.)

    than goes inputs for ProductVariant (name, description, price, etc.)

    than goes inputs for another ProductVariant.

</form>

The Question 问题

Am I done all relations correctly? 我是否正确处理了所有关系? How should I store this data in DB in Controller? 如何将这些数据存储在Controller中的DB中?

Also, I have Controllers for Product, ProductVariant, ProductType. 另外,我还有用于Product,ProductVariant,ProductType的控制器。


I will be very grateful if someone can help me 如果有人可以帮助我,我将非常感激

It's too long to write whole code here or describe how to save relational data in laravel. 在这里编写整个代码或描述如何在laravel中保存关系数据太长了。

Please have a look official documentation and this small tutorial 请看看官方文档和这个小教程

Connected with correct relationships 保持正确的关系

Depending on your example I think that you need to have additional description fields for tables Product and ProductVariant , to store short info about product or concrete variant of product. 根据您的示例,我认为您需要表ProductProductVariant其他description字段,以存储有关产品或产品具体变体的简短信息。

Also I think that will be a lot of product photos, which will be independent of product variant (will be the same for all variants of that product). 我也认为这将是很多产品照片,这些照片与产品变体无关(该产品的所有变体都相同)。 To do not have duplicates of photos for each variant you can change ProductPhotos table structure: 要没有每个变体的照片重复,可以更改ProductPhotos表结构:

  • id ID
  • product_id - belongs to Product product_id- 属于产品
  • product_variant_id - belongs to ProductVariant (NULL if its for all variants of that product) product_variant_id- 属于ProductVariant(对于该产品的所有变体,则为NULL)
  • photo_url photo_url

So to get all photos for concrete variant of product you can get 因此,要获取产品具体变型的所有照片,您可以获得

  • all photos for that product which are not connected with variant ( product_variant_id IS NULL ) 该产品的所有未与变体相关的照片( product_variant_id IS NULL
  • plus all photos for that variant 加上该变体的所有照片

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

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