我有以下情况我现在无法解决:
假设我正在创建一个网店,所以我有产品(产品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创建了这样的模型:
<?PHPclass Product extends Eloquent { public function terms() { return $this->belongsToMany('Term','product_terms','product_ID','term_ID'); }}<?PHPclass Taxonomy extends Eloquent { public function terms() { return $this->hasMany('Term'); }}<?PHPclass Term extends Eloquent { public function taxonomy() { return $this->belongsTo('Taxonomy'); }}
现在我想做以下事情,我想获得所有产品,循环显示它们并显示名称,描述和价格等内容.
好吧,简单的$products = Product :: all();我所需要的.
然后,当我遍历产品时,我知道我可以做一些类似于$product->条款的东西来获得所有条款.但是如何获得给定分类的术语呢?例如,像这样的事情:
<?PHPecho $product->term('brand'); // Where 'brand' is the taxonomy name of the term I want to getecho $product->term('color'); // Where 'color' is the taxonomy name of the term I want to get
希望可以有人帮帮我.
解决方法 好吧,我想我理解你的设置.鉴于您所说的关系,我认为您将模型定义如下:<?PHPclass Product extends Eloquent { public function terms() { return $this->belongsToMany('Term')->withTimestamps(); }}<?PHPclass Taxonomy extends Eloquent { public function terms() { return $this->hasMany('Term'); }}<?PHPclass 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;总结
以上是内存溢出为你收集整理的php – Laravel Eloquent很多很多如何全部内容,希望文章能够帮你解决php – Laravel Eloquent很多很多如何所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)