如何查询中间多对多模型(pivot)中的关系?

问题描述 投票:0回答:0

我正在尝试进行 Eloquent 查询,该查询进入自定义中间模型并按该中间表模型内的关系过滤结果。

让我描述一下结构并展示我的尝试:

型号:

InfluencerProfileInfo extends Model
{
    public function socialMedias()
    {
        return $this->belongsToMany(SocialMedia::class)
            ->withPivot('id','influencer_social_media_type_id', 'url', 'followers')
            ->using(InfluencerSocialMedia::class);
    }
}

------------------------
|id   name    age  ... |
|                      |
------------------------
SocialMedia extends Model
{
    public function influencerProfileInfos()
    {
        return $this->belongsToMany(InfluencerProfileInfo::class)
            ->withPivot('id','influencer_social_media_type_id', 'url', 'followers')
            ->using(InfluencerSocialMedia::class);
    }
}

------------
|id   name |
|          |
------------

InfluencerSocialMedia extends Pivot
{
    public function influencerProfileInfo()
    {
        return $this->belongsTo(InfluencerProfileInfo::class, 'influencer_profile_info_id', 'id');
    }

    public function socialMedia()
    {
        return $this->belongsTo(SocialMedia::class, 'social_media_id', 'id');
    }

    public function reachedAuditories()
    {
        return $this->hasMany(InfluencerSocialMediaReachedAuditory::class, 'influencer_social_media_id', 'id');
    }
}

-------------------------------------------------------------------------
|id   influencer_profile_info_id    social_media_id    url    followers |
|                                                                       |
-------------------------------------------------------------------------
InfluencerSocialMediaReachedAuditory extends Model
{
    public function influencerSocialMedia()
    {
        return $this->belongsTo(InfluencerSocialMedia::class, 'influencer_social_media_id', 'id');
    }

    public function auditory()
    {
        return $this->belongsTo(Auditory::class, 'auditory_id', 'id');
    }
}


---------------------------------------------------------------
|id   influencer_social_media_id    auditory_id   percent(int)|
|                                                             |
---------------------------------------------------------------

我只想获取具有 socialMedias 的配置文件,并且在该 socialMedias 上他们已经达到超过 10 的听觉百分比并且 auditory_id 是 2

我试过:

$profiles = InfluencerProfileInfo::whereHas('socialMedias', function ($query) {
     // On this line I got only profileInfos that have at least one socialMedia witch is correct

     // $query->where('followers', '>', 222000); I can search on column that is on the pivot

     $query->whereHas('reachedAuditories', function ($q) { // this row gives error: Call to undefined method App\Models\SocialMedia::reachedAuditories()
                $q->where('auditory_id', 2)->where('percent', '>', 10);
            });
      })->get();

我可以做到,所以关系声明没有问题:

$profiles[0]->socialMedias[0]->pivot->reachedAuditories()->where('auditory_id', $auditoryId)->get();

$infSocialMedia = new InfluencerSocialMedia();
$infSocialMedia->reachedAuditories()->create(['influencer_social_media_id' => $infSocialMedia->id, 'auditory_id' => 4, 'percent' => 47]);
laravel eloquent model pivot relationship
© www.soinside.com 2019 - 2024. All rights reserved.