Laravel Eloquent - Where In All

You'll have to add multiple whereHas for that:

$query = User::with('activities');
foreach($selectedActivities as $activityId){
    $query->whereHas('activities', function($q) use ($activityId){
        $q->where('id', $activityId);
    });
}
$userByActivities = $query->get();

If you are getting Cardinality violation: 1241 Operand should contain 2 column(s) the problem is the nested selectCount adds to the normal select count(*) instead of overriding the existing select, so changing to $query->distinct()->whereIn('id', $selectedActivities); did the trick for me, or changing to $query->select(DB::raw(count(distinct id)))