How to build SQL query in Laravel 5 using join and whereNotIn in controller?

I need to create the following request in Laravel Controller. Request:

select * from table2 where id not in (select table2.id from table2 inner join table1 on table2.id = table1.id)

      

I did this:

$cond = DB::table('table2')
->whereNotIn('id', function($query){
     $query->select(DB::raw('table2.id'))
     ->from('table2 inner join table1 on table2.id = table1.id');
})->get();

      

Please help me.

Thank you in advance

+3


source to share


1 answer


$cond = DB::table('table2')->whereNotIn('id', function($sq) { 
    $sq->select('table2.id')
       ->from('table2')
       ->join('table1', 'table2.id', '=', 'table1.id'); 
})->get();

      



+1


source







All Articles