我如何在查询生成器 laravel 中执行此操作 SQL

How can I do this SQL in Query builder laravel

如何在查询生成器中执行此操作 SQL laravel

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

试试这个

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

如有疑问,如果您知道原始 SQL,您可以简单地执行

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

此外,所有功劳都归功于这位天才How to create a subquery using Laravel Eloquent?

更新

缺少的参数来自您正在使用的闭包

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

您需要像这样传递 $id 变量

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

再试试看。