Laravel:产品、类别和子类别!(关系船)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51295382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:53:29  来源:igfitidea点击:

Laravel: Products,Categories and SubCategories! (Relation ships)

phpmysqllaraveleloquent

提问by Someb

I have a problem! I make something but, not work so i will ask u for help! So the problem is

我有个问题!我做了一些东西,但不起作用,所以我会向你寻求帮助!所以问题是

I have this tables

我有这张桌子

products:
  name,subcategory_id,category_id,price,description
Categories:
  name,slug,timestamps
SubCategories:
  name,slug,timestamps

I want to make when smo call this url /category/{category}/{subcategory}to get all products of subcategory are called! But when product don't have sub category to open only category , i mean /category/{category}

我想当smo调用这个url/category/{category}/{subcategory}来获取子类别的所有产品被调用!但是当产品没有子类别只能打开类别时,我的意思是/category/{category}

Thanks guys!

谢谢你们!

回答by Someb

Here i may help u with this!

在这里我可以帮助你!

First , you can use only one table for categories and subcategory!

首先,类别和子类别只能使用一个表!

You can make this:

你可以这样做:

 |---------------------------------------
 |id| |parent_id|    |name|       |slug|
 |1 | | (NULL)  | Electronics | electronics
 |2 | | 1       | Phones      | phones

Now category electronic have children Phones

现在品类电子有孩子 Phones

So in your Category.phpmodel u can make that

所以在你的Category.php模型中你可以做到

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent() {
    return $this->belongsTo(self::class, 'parent_id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function children() {
    return $this->hasMany(self::class, 'parent_id','id');
}

Now u can use foreach to display your children and category! Hope that helpful! ;)

现在您可以使用 foreach 来显示您的孩子和类别!希望有帮助!;)

回答by Rafa? Sardaw

It's classic problem related with databases You should use hierarchical child/parent relation. At first Your design of database is bad, just add one column with root category which has "0" in PARENT_ID.

这是与数据库相关的经典问题您应该使用分层的子/父关系。一开始你的数据库设计很糟糕,只需添加一个根类别在PARENT_ID中为“0”的列。

CREATE TABLE CATEGORIES ( 
      CATEGORY_ID  NUMBER,
      PARENT_ID    NUMBER,
      NAME         VARCHAR(255), 
      CREATE_TS    TIMESTAMP(0));

and then read this : http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/& How to create a MySQL hierarchical recursive query& Bear in mind that You are mixing logic of GUI with SQL role. If You want to return in single query then Your presentation layer will be quite ugly ;-)

然后阅读此内容:http: //mikehillyer.com/articles/managing-hierarchical-data-in-mysql/& 如何创建 MySQL 分层递归查询& 请记住,您正在将 GUI 逻辑与 SQL 角色混合。如果您想在单个查询中返回,那么您的表示层将非常难看 ;-)

and also think about leaving just category_id in product table, if You have only 2 or 3 levels You don't need to point to subcategory and category.

并考虑在产品表中只留下 category_id,如果您只有 2 或 3 个级别,则不需要指向子类别和类别。