我想学习Laravel,目前正在尝试使用Eloquent进行实验。
我遇到了一个无法解决的问题:
假设我正在创建一个网店,所以我有商品(Product 1,Product 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
希望有人能够帮助我。
$products = Product::all();
返回的所有产品进行 foreach 循环后的结果,就像foreach($product as $product) { // your code }
这样。 - Crinsane