在Laravel 5 Eloquent中加入和关于孩子的where子句

时间:2015-04-14 09:02:07

标签: eloquent laravel-5 jointable

我正在使用Laravel 5开发一个应用程序,我有2个表productCategoryproduct表有一个category_id列引用{{1} } id

我还有2个模型catgeoryCategory

问题: 我只需要在我的Product模型中添加一个简单的函数,该函数返回由Category带有雄辩的产品加入的类别

1 个答案:

答案 0 :(得分:1)

您只需添加普通关系方法并附加where条件:

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