Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Query Builder WHERE NOT IN

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.

like image 794
Tommy Lee Avatar asked Feb 06 '15 08:02

Tommy Lee


2 Answers

You can use whereNotIn with a closure:

$result = DB::table('exams')->whereNotIn('id', function($q){
    $q->select('examId')->from('testresults');
})->get();
like image 96
lukasgeiter Avatar answered Sep 30 '22 16:09

lukasgeiter


with Eloquent :

$result = Exams::whereNotIn('id', function($q){
        $q->select('examId')->from('testresults');
    })->get();
like image 36
Hamidreza Akbari Avatar answered Sep 30 '22 18:09

Hamidreza Akbari