Q:

Building SQL query in eloquent

how would I go about writing this SQL query in Laravel 4.2 using eloquent?
SELECT *
FROM participants
WHERE user_id IN (1, 2) 
GROUP BY convo_id
HAVING count(DISTINCT user_id) = 2
AND count(DISTINCT convo_id) = 1

I tried this already
Participant::whereIn('user_id', $participantIds)
                    ->groupBy('convo_id')
                    ->havingRaw('count(DISTINCT user_id) = '. sizeof($participantIds))
                    ->whereRaw('count(DISTINCT convo_id) = 1')
                    ->get();

But I get this error
SQLSTATE[HY000]: General error: 1111 Invalid use of group function (SQL: select * from `participants` where `user_id` in (1, 2, 4) and count(DISTINCT convo_id) = 1 group by `convo_id` having count(DISTINCT user_id) = 3)

A:

My mistake. whereRaw should also be havingRaw.
Participant::whereIn('user_id', $participantIds)
                    ->groupBy('convo_id')
                    ->havingRaw('count(DISTINCT user_id) = '. sizeof($participantIds))
                    ->havingRaw('count(DISTINCT convo_id) = 1')
                    ->get();

