In Laravel 4.2, I am trying to achieve a query that returns all users, that have all of certain activities. As of now, I have a query that returns all users that have one of many activities:
//$selectedActivities being an array
$userByActivities = User::with('activities')
->whereHas('activities', function($query) use($selectedActivities){
$query->whereIn('id', $selectedActivities);
})->get();
To be more clear: given activities a,b,c. I am looking for all users that have activity a AND b AND c. My query returns all users that have activity a OR b OR c.
@musaya Yeah, lukasgeiter knows it as well. However this time his solution is overkill. It adds subquery for each id you want to check. The way I showed is what you should do here.
I have exact same problem but for me, lukasgeiter's solution on SO works but the accepted solution here gives me Cardinality violation: 1241 Operand should contain 2 column(s). I'd prefer to reduce the number of queries using @JarekTkaczyk's elegant solution. Any ideas? Let me know if you need more info.