Laravel Eloquent - 多对多,通过,

时间:2018-03-20 19:35:09

标签: php laravel eloquent many-to-many

我有三个表:产品,类别和product_has_category

类别表有一个“类型”字段,可以是“性别”或“服装”。因此,产品有许多类别,一个类别有很多产品。

更棘手的部分是如何我有两种不同类型的类别(即性别和服装)。产品只能有一个“性别”类别,只有一个“服装”类别。

产品表:

---------------
| 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_has_category表:

----------------------------
| product_id | category_id |
----------------------------
| 1          | 1           |
----------------------------
| 1          | 3           |
----------------------------
| 2          | 2           |
----------------------------
| 2          | 5           |
----------------------------

因此,根据以上数据,我们有:

Style 1是男士的圆领,Style 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);

我想我理解如何使用将交汇表设置为“product_has_category”的belongsToMany()方法在我的模型中建立标准的多对多关系。

在我的类别模型中,我有以下关系:

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');
    }
}

有人能指出我如何设置这些类型的数据关系的正确方向吗?

1 个答案:

答案 0 :(得分:2)

我假设您的关系按预期工作。

这是你的问题:

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'...)),您正在处理相关模型的查询。因此,您需要删除category.部分原因,因为您已经在处理类别查询。

像这样:

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(),则会将这两个类别分开。