在我的控制器中,我设置了两个过滤器,一个用于类别(has-one关系),另一个用于商店(多对多关系)。
public function products($filter = NULL, $key = NULL)
{
$query = Product::query();
$query = $query->where('status', '=', 1);
if ($filter === 'category') {
$cat = Category::where('slug', '=', $key)->select('id')->firstOrFail();
$query = $query->where('category_id', '=', $cat->id);
}
if ($filtro === 'shop') {
$shop = Shop::where('slug', '=', $key)->select('id')->firstOrFail();
// HOW TO ADD HERE A WHERE IN MANY TO MANY RELATIONSHIP*
}
$products = $query->whereNotNull('startdate')->with('shops', 'categories')->orderby('startdate')->get();*如果设置了"shop“筛选器,如何添加到查询A WHERE IN多对多关系中?
发布于 2018-03-08 20:47:41
像这样使用whereHas()方法:
$query->whereHas('shops', function($q) use($shop) {
$q->where('id', $shop->id);
});在Product模型中定义shops关系以实现此目的。
https://stackoverflow.com/questions/49173466
复制相似问题