2015-04-14 100 views
1

我开发一个应用程序与Laravel 5,我有2个表productCategory,该product表中有一栏category_id是指idcatgeory加入和where子句对孩子Laravel 5雄辩

我也有2种型号CategoryProduct

问题: 我只需要一个简单的功能添加到我的Category模型,返回由产品加入了类别,其中product.sx_code = 0与雄辩

回答

1

您可以再补充一个正常的关系方法,并附加where条件:

public function specialProducts(){ 
    return $this->hasMany('App\Product')->where('sx_code', 0); 
} 
+0

如果什么sx_code的值是一个变量? –

+0

@JadJoubran然后你不能把它放在关系声明中。你需要在查询时应用它:'$ category-> specialProducts() - > where('sx_code',$ code) - > get()' – lukasgeiter

+1

好吧,但在我的情况下,我需要指定列名称.. ('table.sx_code',$ code) - > get();' –