我正在努力学习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 | | +-------------+------------------+------+-----+---------------------+----------------+
这些分类法有条款(耐克,阿迪达斯,红色,绿色,男孩,女孩):
+-------------+------------------+------+-----+---------------------+----------------+ | 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->条款的东西来获得所有条款.但是如何获得给定分类的术语呢?例如,像这样的事情:
<?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
希望可以有人帮帮我.
解决方法
好吧,我想我理解你的设置.鉴于您所说的关系,我认为您将模型定义如下:
<?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(); } }
由于您的pivot已正确命名并且id,因此您不必在belongsToMany关系中指定该信息,但如果您希望时间戳工作,则需要使用withTimestamps.
编辑:
如果您可以通过id而不是slug来查看分类法,这将起作用:
$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;