我正在使用Laravel 5开发一个应用程序,我有2个表product
和Category
,product
表有一个category_id
列引用{{1} } id
表
我还有2个模型catgeory
和Category
问题:
我只需要在我的Product
模型中添加一个简单的函数,该函数返回由Category
带有雄辩的产品加入的类别
答案 0 :(得分:1)
您只需添加普通关系方法并附加where
条件:
public function specialProducts(){
return $this->hasMany('App\Product')->where('sx_code', 0);
}