2015-04-14 112 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();' –