Fayakon
Fayakon

Reputation: 1523

convert SELECT query from mySql to Laravel query builder

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

Answers (2)

Dhruv Raval
Dhruv Raval

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

Kelvin
Kelvin

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

Related Questions