我正在尝试学习 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
希望可以有人帮帮我。