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

Solution:

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