Laravel 一件商品的条件在哪里?

Laravel where condition for one item?

我有 Products table 和 Categories table。

每个产品可能有一个或多个类别。为此我创建了 table: products_caetgories.

那么,如何设置 where 条件 product_category = 1?

我试过:

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

看看whereHas

you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries. These methods allow you to add customized constraints to a relationship constraint, such as checking the content of a comment:

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