【发布时间】:2013-04-24 18:57:53
【问题描述】:
对于我正在开发的 Laravel 应用程序,我想用我的 Eloquent 模型做这样的事情:
$product = Product::with('terms')->find(1);
$brand = $product->terms('brand');
$color = $product->terms('color');
条款是多对多的关系。在这种情况下,术语是分类学术语。因此,产品术语可能是:Nike、Red、Boys 等。
如果我使用$product->terms,我会得到所有条款,而当我使用$product->terms('brand') 时,我会得到'Nike'。
现在我的产品模型是这样的:
class Product extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function terms($taxonomy)
{
return $this->belongsToMany('Term', 'productterms');
}
}
是否有可能做我想要实现的目标?
【问题讨论】:
标签: php laravel relationship laravel-4 eloquent