mahmoud310
mahmoud310

Reputation: 300

How can I do this SQL in Query builder laravel

How can I do this SQL in Query builder laravel

select Doctor_id from doctors where Doctor_id NOT IN
 (SELECT Doctor_id from report_reviewers WHERE Report_id = 26 )

Upvotes: 2

Views: 2764

Answers (1)

EddyTheDove
EddyTheDove

Reputation: 13259

Try with this

$result = DB::table('doctors')
->whereNotIn('doctor_id', function($q){
    $q->from('report_reviewers')
    ->select('Doctor_id')
    ->where('Report_id', '=', 26)
})
->select('doctor_id')
->get();

Whenver in doubt, if you know the raw SQL, you can simply do

$query = 'select Doctor_id from doctors ...';
$result = DB::select($query);

Also, all credit goes to this genius How to create a subquery using Laravel Eloquent?

Update

The missing argument comes from the closure you are using

->whereNotIn('doctor_id', function($q,$id){ 
    $q->from('report_reviewers') 
    ->select('Doctor_id') 
    ->where('Report_id',$id); 
}) 

You need to pass the $id variable like this

->whereNotIn('doctor_id', function($q) use ($id) { 
    $q->from('report_reviewers') 
    ->select('Doctor_id') 
    ->where('Report_id',$id); 
}) 

Try again and see.

Upvotes: 2

Related Questions