#HasManyThrough counts inside count

1 messages · Page 1 of 1 (latest)

late coral
#

Im using a relation in my model

    public function hotLeads(): HasManyThrough
    {
        return $this->hasManyThrough(
            Lead::class,
            Person::class,
            'id',
            'person_id',
            'person_id',
            'id'
        )
            ->where('validated', 1)
            ->whereNotIn('status', [Status::LEAD_CLOSED->value, Status::SEAL_LEAD_CLOSED->value]);
    }```

This relation takes me to multiple models which is fine. 
when i want to count this i just say

withCount('hotLeads')

and i get the correct result.

1. The issue that im trying to solve is that a lead model has a vehicles relation ```hasMany```
and im wanting to count those vehicles inside the ```hotLeads``` relation.
If i do a withCount i dont get the correct number.

Its almost like i need to sum up all the counts of the vehicles relation.

Here is where i have got to:

    $queryBuilder->withCount([
        'hotLeads as hot_leads_count',
        'hotLeads as vehicle_count' => function($query) {
            $query->withCount('vehicles'); -> need to sum these counts
        }
    ]);
late coral
#

tried ```

    $queryBuilder->withCount([
        'hotLeads as hot_leads_count'
    ])->with(['hotLeads'=>function($query) {
        $query->whereHas('vehicles')->withCount('vehicles')->get()->sum('vehicles_count');
    }]);
#

wish i could do withCount('hotLeads.vehicles')