Laravel 5.8 局部查询范围与枢轴 table、return 相关模型

Laravel 5.8 local query scope with pivot table, return the related models

我有 Services 作为联系人与 People 有多对多关系,通过 services_contacts 枢轴 table 连接,我正在尝试在 Service 模型上创建本地范围查询到 return primaryContacts():

public function scopePrimaryContacts($query)
{
    $pivot = $this->contacts()->getTable();

    return $query->whereHas('contacts', function ($q) use ($pivot) {
        return $q->where("{$pivot}.is_primary", true);
    });
}

这是 return 的服务,我需要它 return 与枢轴 table 本身相关的 is_primary 人员。我希望能够在我的 Service 模型上调用 $this->primaryContacts,就像我可以调用 $this->contacts 来获取 any/all 联系人一样。任何想法从这里去哪里?以下是关系...contactsService 模型上:

public function contacts()
{
    return $this->belongsToMany(Person::class, 'services_contacts', 'service_uuid', 'person_uuid')
        ->withPivot('is_primary', 'is_active', 'contact_type_uuid')
        ->withTimestamps();
}

servicesPerson 模型上:

public function services()
{
    return $this->belongsToMany(Service::class, 'services_contacts', 'person_uuid', 'service_uuid');
}

我不会将其设为作用域,我会制作第二个关系函数,但带有一些额外的参数。

服务模式

public function primaryContact() 
{
    return $this->belongsToMany(Person::class, 'services_contacts', 'service_uuid', 'person_uuid')
            ->wherePivot('is_primary', true)
            ->wherePivot('is_active', true);
}