我正在尝试在我的 Laravel 8.0 应用程序中获取多对多关系的关系计数。其中有 project_id、product_group_id、product_subgroup_id 和 brand_id,所以我的表如下所示:
project_id product_group_id product_subgroup_id brand_id
1 1 1 1
1 1 1 2
1 2 3 1
在我的模型品牌中,我将关系定义如下:
public function projects()
{
return $this->belongsToMany(Project::class, 'project_associate_brand', 'brand_id', 'project_id');
}
同样我在项目模型中定义了关系:
public function brand()
{
return $this->belongsToMany(Brand::class, 'project_associate_brand', 'project_id', 'brand_id');
}
我正在尝试过滤没有特定品牌关系的项目列表
为此我使用过
->whereDoesntHave('brand', function ($q) {
$request_brand_doesnt_have = request()->has('_token') ? json_decode(request('brand_doesnt_have')) : request('brand_doesnt_have');
$q->whereIn('project_associate_brand.brand_id', collect($request_brand_doesnt_have)->pluck('id'));
});
但我的项目计数不正确