Laravel Query Builder WHERE NOT IN

Tommy Lee picture Tommy Lee · Feb 6, 2015 · Viewed 19.5k times · Source

I have the following sql query

SELECT * FROM exams WHERE exams.id NOT IN (SELECT examId FROM testresults)

how can I convert it into Laravel query builder format?

Thanks.

Answer

lukasgeiter picture lukasgeiter · Feb 6, 2015

You can use whereNotIn with a closure:

$result = DB::table('exams')->whereNotIn('id', function($q){
    $q->select('examId')->from('testresults');
})->get();