1

我需要brands在特定collection页面中获取数据,但只有超过 1 个product

这是模型之间的关系。

Brand -> HasMany -> Product

Product <= BelongsToMany => Collection

我能够获得所有系列中包含超过 1 种产品的品牌数据,如下所示:

$brands = Brand::has("products")->get(); //this will return all brands that have more than 1 product.

现在我需要在这里添加收藏限制。

我可以从$slug特定页面获取集合。

$collection = Collection::where("slug", $slug)->first();

谁能帮我如何获取特定收藏页面的品牌?

4

1 回答 1

1

试试这个:

$brands = Brand::has("products")
->whereHas('products.collections',function($q) use ($slug){ // your relation to product model
  $q->where("slug", $slug);
})
->get();
于 2021-08-02T07:16:52.747 回答