Php 拉雷维尔雄辩-多对多,通过,在哪里
我有三个表:产品、类别和产品类别 “类别”表有一个“类型”字段,它是“性别”或“服装”。所以一个产品有很多种类,一个种类有很多产品 更棘手的是,我有两种不同的类别(即性别和服装)。一个产品只能有一个“性别”类别和一个“服装”类别 产品表:Php 拉雷维尔雄辩-多对多,通过,在哪里,php,laravel,eloquent,many-to-many,Php,Laravel,Eloquent,Many To Many,我有三个表:产品、类别和产品类别 “类别”表有一个“类型”字段,它是“性别”或“服装”。所以一个产品有很多种类,一个种类有很多产品 更棘手的是,我有两种不同的类别(即性别和服装)。一个产品只能有一个“性别”类别和一个“服装”类别 产品表: --------------- | id | style | --------------- | 1 | Style 1| --------------- | 2 | Style 2| --------------- 类别表: ------------
---------------
| id | style |
---------------
| 1 | Style 1|
---------------
| 2 | Style 2|
---------------
类别表:
----------------------------
| id | type | name |
----------------------------
| 1 | gender | men's |
----------------------------
| 2 | gender | women's |
----------------------------
| 3 | garment | crew neck |
----------------------------
| 4 | garment | v neck |
----------------------------
| 5 | garment | tank top |
----------------------------
产品类别表:
----------------------------
| product_id | category_id |
----------------------------
| 1 | 1 |
----------------------------
| 1 | 3 |
----------------------------
| 2 | 2 |
----------------------------
| 2 | 5 |
----------------------------
因此,根据上述数据,我们有:
样式1为男士圆领,样式2为女士背心
我希望能够以这种方式检索产品:
// returns Style 1, men's, crew neck
$product = Product::with(['gender', 'garment'])->find(1);
// returns Style 2, women's, tank top
$product = Product::with(['gender', 'garment'])->find(2);
我想我理解了如何使用belongToMany()方法在模型中设置标准的多对多关系,将连接表设置为“product\u has\u category”
在我的类别模型中,我有以下关系:
class Category extends Model
{
public function products()
{
return $this->belongsToMany('App\Product', 'product_has_category', 'category_id', 'product_id');
}
}
但我不知道如何设置产品模型中的关系,以获得给定类别类型的类别。这是我的产品模型中的内容,这在某种程度上是有意义的,但是laravel抛出了一个错误,关于category.type是一个未知列
class Product extends Model
{
public function gender()
{
return $this->belongsToMany('App\Category', 'product_has_category', 'product_id', 'category_id')->where('type', '=', 'gender');
}
public function garment()
{
return $this->belongsToMany('App\Category', 'product_has_category', 'product_id', 'category_id')->where('type', '=', 'garment');
}
}
有人能为我指出如何建立这些类型的数据关系的正确方向吗?我假设您的关系按预期工作 这是你的问题:
public function gender()
{
return $this->belongsToMany('App\Category', 'product_has_category', 'product_id', 'category_id')
->where('category.type', '=', 'gender'); // Here
}
public function garment()
{
return $this->belongsToMany('App\Category', 'product_has_category', 'product_id', 'category_id')
->where('category.type', '=', 'garment'); // And here
}
当您将查询从关系(在您的案例中是->where('category.type')
中链接出来时,您正在处理相关模型的查询。因此,您需要删除类别。
部分,因为您已经在处理类别查询
像这样:
public function gender()
{
return $this->belongsToMany('App\Category', 'product_has_category', 'product_id', 'category_id')
->where('type', '=', 'gender'); // Removed 'category.'
}
public function garment()
{
return $this->belongsToMany('App\Category', 'product_has_category', 'product_id', 'category_id')
->where('type', '=', 'garment'); // Removed 'category.'
}
现在,如果您调用
Product::with(['gender','garment'])->first()
您将把这两个类别分开。您阅读过关于该主题的主题吗?如果是这样,为什么您要将您的数据透视表命名为无论您有什么东西,请使用建议:要定义此关系,需要三个数据库表:用户
、角色
和角色
(最后一个按字母顺序排列)。您确定需要“类别”系统而不是“标签”系统吗系统?谢谢Kyslik!是的,我已经阅读了文档。我正在使用较旧的数据库,不希望更改表名。文档中有一节介绍了laravel如何采用字母顺序的透视表名,但我们可以在belongToMany()中自由重写该约定并自定义透视表名关系。如果它被认为是一个“类别”系统或“标签”系统,它仍然不能解决如何会有不同的“类型”类别或不同的“类型”我不认为是标签的问题。但也许我误解了你的问题/建议。从长远来看,这将对你有所帮助。谢谢devk!那确实是一个愚蠢的错误,不值得发表一篇文章。你做到了。非常感谢你的帮助!