Hey folks! I’m struggling to convert a Microsoft SQL Server 2008 query to Laravel 4.2 syntax. I’m pretty new to Laravel and could use some help.
Here’s what I’ve got so far in Laravel:
$results = DB::table('StudentLogs')
->select('log_data', 'log_date', 'student_id')
->where('action_type', 200)
->where('section_id', $sectionId)
->whereIn('log_date', function($query) use ($sectionId, $classId) {
$query->select(DB::raw('MIN(log_date)'))
->from('StudentLogs')
->where('log_data', '!=', '0')
->where('action_type', 200)
->where('section_id', $sectionId)
->whereIn('student_id', function($subquery) use ($classId) {
$subquery->select('student_id')
->from('ClassEnrollments')
->join('Students', 'Students.id', '=', 'ClassEnrollments.student_id')
->where('class_id', $classId);
})
->groupBy('student_id');
})
->orderBy('log_data', 'desc')
->take(10)
->get();
I’m not sure if this is correct or efficient. Can someone help me optimize this query or point out any mistakes? Thanks in advance!