2013-04-04 100 views
4

我想学习Laravel,现在我正在试验雄辩。 我有,我不能现在解决下面的情况:Laravel雄辩许多到许多如何

比方说,我创建一个网上商店,所以我有产品(产品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 |    | 
+-------------+------------------+------+-----+---------------------+----------------+ 

这些分类有条款(耐克,阿迪达斯,红色,绿色,男孩,女孩):

+-------------+------------------+------+-----+---------------------+----------------+ 
| 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 |    | 
+------------+------------------+------+-----+---------------------+----------------+ 

我已经创建模型产品,分类和术语是这样的:

<?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 

希望有人能帮助我。

回答

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

} 

由于您的支点被命名为正确id'd你不应该有指定的belongsToMany关系该信息,但如果你想要时间戳工作,你需要withTimestamps。

编辑:

如果你可以通过ID而不是塞看起来分类了,这将工作:

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

在上述情形你可以只存储分类塞在术语表因为它应该是一个独特的关键。否则,你可以看看在条款并寻找您想要的与分类:

$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; 
+0

谢谢你的回答,我今天晚些时候会看看它。但只是一个快速清理,为什么我有关于产品的条款。分类只是一组术语的名称。你有像'红色,蓝色,绿色,黄色'的术语和这些术语的分类是'颜色'。对于一个产品我想获得它的“条款”。不是它的分类法。因为通常情况下,产品将具有所有分类法。我对它具有的给定分类学的哪些术语感兴趣。 “什么颜色的产品”,“什么品牌的产品” – Crinsane 2013-04-05 17:39:40

+0

噢,顺便说一句,最后两个表确实相同,但这是一个错误,马上更新它。 – Crinsane 2013-04-05 17:42:50

+0

我无法得到这个工作。获取此错误: 调用未定义的方法Illuminate \ Database \ Query \ Builder :: taxonomy()' – Crinsane 2013-04-05 21:30:57