Reputation: 1523
what would be Laravel Query Builder code of this mysql query;
SELECT *
FROM Student
WHERE family_id IN (SELECT family_id
FROM students
GROUP BY family_id
HAVING COUNT(1)>1)
ORDER BY family_id
Upvotes: 1
Views: 84
Reputation: 1583
Use function in whereIn clause:
Student::whereIn('family_id', function($query) {
$query->select('family_id')
->from(with(new Students)->getTable())
->groupBy('family_id')
->havingRaw('COUNT(1) > 1')
})->get();
Upvotes: 0
Reputation: 1114
$query = DB::select("SELECT *
FROM Student
WHERE family_id IN (SELECT family_id
FROM students
GROUP BY family_id
HAVING COUNT(1)>1)
ORDER BY family_id")
Based on Laravel Documentation
Upvotes: 1