4

我正在尝试学习 Laravel,现在我正在尝试 Eloquent。我有以下情况,我现在无法解决:

假设我正在创建一个网上商店,所以我有产品(产品 1、产品 2 等):

+-------------+------------------+------+-----+---------------------+----------------+
| Field       | Type             | Null | Key | Default             | Extra          |
+-------------+------------------+------+-----+---------------------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| name        | varchar(255)     | NO   |     | NULL                |                |
| slug        | varchar(255)     | NO   |     | NULL                |                |
| description | text             | NO   |     | NULL                |                |
| price       | decimal(5,2)     | NO   |     | NULL                |                |
| active      | tinyint(4)       | NO   |     | 1                   |                |
| created_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+------------------+------+-----+---------------------+----------------+

这些产品按分类法分类(品牌、颜色、部门):

+-------------+------------------+------+-----+---------------------+----------------+
| Field       | Type             | Null | Key | Default             | Extra          |
+-------------+------------------+------+-----+---------------------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| name        | varchar(255)     | NO   |     | NULL                |                |
| slug        | varchar(255)     | NO   |     | NULL                |                |
| description | text             | NO   |     | NULL                |                |
| created_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+------------------+------+-----+---------------------+----------------+

这些分类有术语(Nike、Adidas、Red、Green、Boys、Girls):

+-------------+------------------+------+-----+---------------------+----------------+
| Field       | Type             | Null | Key | Default             | Extra          |
+-------------+------------------+------+-----+---------------------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| taxonomy_id | int(11)          | NO   |     | NULL                |                |
| name        | varchar(255)     | NO   |     | NULL                |                |
| slug        | varchar(255)     | NO   |     | NULL                |                |
| description | text             | NO   |     | NULL                |                |
| created_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at  | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+------------------+------+-----+---------------------+----------------+

最后,我有一个数据透视表来将术语与产品联系起来:

+------------+------------------+------+-----+---------------------+----------------+
| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| product_id | int(11)          | NO   |     | NULL                |                |
| term_id    | int(11)          | NO   |     | NULL                |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+

我已经为 Product、Taxonomy 和 Term 创建了模型,如下所示:

<?php

class Product extends Eloquent {

    public function terms()
    {
        return $this->belongsToMany('Term', 'product_terms', 'product_id', 'term_id');
    }

}

<?php

class Taxonomy extends Eloquent {

    public function terms()
    {
        return $this->hasMany('Term');
    }

}

<?php

class Term extends Eloquent {

    public function taxonomy()
    {
        return $this->belongsTo('Taxonomy');
    }

}

现在我想做以下事情,我想获取所有产品,遍历它们并显示名称、描述和价格之类的东西。好吧,$products = Product::all();我只需要一个简单的。然后,当我遍历产品时,我知道我可以做一些事情,比如$product->terms获取所有条款。但是应该如何获得给定分类法的术语。所以例如这样的事情:

<?php
echo $product->term('brand'); // Where 'brand' is the taxonomy name of the term I want to get
echo $product->term('color'); // Where 'color' is the taxonomy name of the term I want to get

希望可以有人帮帮我。

4

1 回答 1

3

好吧,我我明白你的设置了。鉴于您所说的关系,我认为您将模型定义如下:

<?php

class Product extends Eloquent {

    public function terms()
    {
        return $this->belongsToMany('Term')->withTimestamps();
    }

}

<?php

class Taxonomy extends Eloquent {

    public function terms()
    {
        return $this->hasMany('Term');
    }

}

<?php

class Term extends Eloquent {

    public function taxonomy()
    {
        return $this->belongsTo('Taxonomy');
    }

    public function products()
    {
        return $this->belongsToMany('Product')->withTimestamps();
    }

}

由于您的枢轴已正确命名和标识,因此您不必在 belongsToMany 关系中指定该信息,但如果您希望时间戳工作,您需要 withTimestamps。

编辑:

如果您可以通过 id 而不是 slug 查看分类法,这将起作用:

$products = Product::all();
foreach($products as $product)
{
    $term = $product->terms()->with('taxonomy')->where('taxonomy_id', '=', 2)->first();
    echo $term->name;
}

在上面的场景中,您可以只将分类 slug 存储在术语表中,因为它应该是唯一的键。否则,您可以查看术语并查找具有所需分类的术语:

$products = Product::all();
foreach($products as $product)
{
    $terms = $product->terms()->with('taxonomy')->get();
    foreach($terms as $term)
    {
        if($term->taxonomy->slug == 'brand')
        {
            echo $term->name."<br />";
        }
    }
}

结束编辑

我不得不承认我有点迷茫,因为你的最后两张表看起来完全一样,我想知道你为什么将术语与产品联系起来,而不是将分类法与产品联系起来。我怀疑我的解决方案不适用于您关联事物的方式,但如果您将分类法与产品关联起来,它会很好地工作。

<?php
$product->taxonomy()->where('slug', '=', 'brand')->term;
于 2013-04-05T14:20:19.090 回答