无法在Laravel中联接表后获取关系数据

无法在Laravel中联接表后获取关系数据,laravel,join,eloquent,Laravel,Join,Eloquent,我正在Laravel上构建一个应用程序,其中有一个名为ConstructionProduct的模型,类似这样: class ConstructionProduct extends Model { protected $table='construction_product'; public function productDetails() { return $this->hasMany(ConstructionProductDetails::c

我正在Laravel上构建一个应用程序,其中有一个名为
ConstructionProduct
的模型,类似这样:

class ConstructionProduct extends Model {

    protected $table='construction_product';

    public function productDetails()
    {
        return $this->hasMany(ConstructionProductDetails::class, 'construction_product_id', 'id');
    }
    
}
class ConstructionProductDetails extends Model {

    protected $table='construction_product_details';
    
     public function constructionProduct()
    {
        return $this->belongsTo(ConstructionProduct::class, 'construction_product_id', 'id');
    }

    public function brand()
    {
        return $this->belongsToMany(Brand::class, 'construction_product_brands', 'const_product_details_id', 'brand_id');
    }
}
construction\u product
中有列:
id
处创建

然后我将
ConstructionProductDetails
建模如下:

class ConstructionProduct extends Model {

    protected $table='construction_product';

    public function productDetails()
    {
        return $this->hasMany(ConstructionProductDetails::class, 'construction_product_id', 'id');
    }
    
}
class ConstructionProductDetails extends Model {

    protected $table='construction_product_details';
    
     public function constructionProduct()
    {
        return $this->belongsTo(ConstructionProduct::class, 'construction_product_id', 'id');
    }

    public function brand()
    {
        return $this->belongsToMany(Brand::class, 'construction_product_brands', 'const_product_details_id', 'brand_id');
    }
}
施工产品\u详细信息
有列:
id
施工产品\u id

我有一个表
construction\u product\u project
,列名为:
id
construction\u product\u id
project\u id
另外,我还有另一个表
construction\u product\u brands
,列名称为:
id
const\u product\u details\u id
brand\u id

我正在尝试执行以下查询:

return ConstructionProductDetails::whereHas('brand', function ($q) use($id){
        $q->where('construction_product_brands.brand_id', $id);
    })
    ->with('brand')
    ->paginate();
这给了我品牌关系,但当我加入
建筑产品项目
时,我得到的是空的或空的品牌列表。以下是我的疑问:

return ConstructionProductDetails::whereHas('brand', function ($q) use($id){
        $q->where('construction_product_brands.brand_id', $id);
    })
    ->join('construction_product_projects', 'construction_product_details.construction_product_id', '=', 'construction_product_projects.construction_product_id')
    ->with('brand')
    ->paginate();

我认为这与
品牌id
相矛盾。我怎样才能得到想要的结果呢?

你能试着把你的
join
放在
whereHas
前面看看会发生什么吗?你能试着把你的
join
放在
whereHas
前面看看会发生什么吗?