2016-09-04 75 views
0

我有Products表和Categories表。Laravel哪裏有一件物品的條件?

每個產品可能有一個或多個附件。爲此我創建了表格:products_caetgories

那麼,我怎樣才能設置where條件哪裏product_category = 1?

我試着像爲:

Product::with("categories")->where("product_category", 1)->get(); 
+0

這http://stackoverflow.com/questions/19745512/laravel-selecting-with條件 - 從多對多關係可能會有所幫助 –

回答

0

TAKLE看看whereHas

您可以使用whereHas和orWhereHas方法把 「其中」 條件對您有疑問。這些方法讓你 自定義添加約束關係的約束,如檢查 評論的內容:

Product::with("categories")->whereHas("categories", function($query){ 
$query->where('id','=',1); 
})->get();