从 laravel 中的 whereHas 关系计算关系

Counting relationship from whereHas relationship in laravel

我正在 Laravel 5.4 中构建一个小型应用程序,我在其中从 whereHas 查询我的关系数据,如下所示:

$interactions  = Company::where('is_client', '=', 1)
    ->whereHas('interactionSummaries', function ($query) {
        $query->whereHas('interaction', function ($query2) {
            $query2->whereHas('contactsAssociation', function($query3) {
                $query3->whereHas('company', function ($query4) {
                    $query4->where('type', 'like', 'Research');
                });
            });
        });
})->get();

以下是我的模型:

公司:

class Company extends Model
{
     public function contacts()
    {
        return $this->belongsToMany('App\Contact', 'company_contact', 'company_id','contact_id');
    }

    public function interactionSummaries()
    {
        return $this->hasMany('App\InteractionSummary', 'company_id');
    }

}

互动总结:

class InteractionSummary extends Model
{
    public function interaction()
    {
        return $this->belongsTo('App\Interaction');
    }   
}

互动:

class Interaction extends Model
{
     public function clientsAssociation()
    {
        return $this->belongsToMany('App\Contact', 'contact_client_interaction',  'interaction_id', 'contact_id')->withPivot('company_id')->withTimestamps();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function contactsAssociation()
    {
        return $this->belongsToMany('App\Contact', 'contact_interaction',  'interaction_id', 'contact_id')->withPivot('company_id')->withTimestamps();
    }

}

联系人:

class Contact extends Model
{
    public function company()
    {
        return $this
            ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
    }

}

我想获取 company 模型的 contactsAssociation 的计数,我该如何实现?我的意思是 withCount('contactsAssociation')

我不推荐这个解决方案,因为它是繁重的查询并且大量内存用于不必要的集合,即使是小数据集也可能成为瓶颈。我建议使用查询构建器和更多 SQL 连接 and/or 子选择的方法。

eloquent 方法是:

// Assuming you want to get sum for the Companies collection
$companies = Company::where('is_client', '=', 1)
    // load count on distant model
    ->with(['interactionSummaries.interaction' => function ($q) {
        $q->withCount(['contactsAssociation' => function ($q) {
            $q->whereHas('company', function ($q) {
                $q->where('type', 'like', 'Research');
            });
        }]);
    }])
    ->get()
    ->transform(function ($company) {
        $company->contacts_association_count = $company->interactionSummaries
                                                       ->pluck('interaction.contacts_association_count')
                                                       ->collapse()
                                                       ->sum();
        return $company;
    });

    // then each Company will have the count as an attribute:
    foreach ($companies as $company) {
        $company->contacts_association_count;
    }