Level 75
A suggestion only, if this query works I'd use as is and not bother with builder.
I have done queries like this, sample data only for a previous reply:
$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
->select('dc_powners.ownerid', 'dc_powners.oname')
->selectRaw('count(dc_pets.petid) as countOfPets')
->groupby('dc_powners.ownerid')
->orderby('dc_powners.oname')
->get();
Results basically give:
ownerid, oname, countOfPets
Like:
5|Bob|3
4|Greg|9
2|Rob|1
Normally I use a little trial and error and a combination of examples in the documentation to work out the query.
But usually things like yours I use regular mysql and pdo queries. And use the lengthaware paginator if needing to paginate.
Also there are times where I query a query.