eloquent哪里没有查询到?

eloquent where not in query?

我正在尝试使用 eloquent 构建以下 sql 查询。查询给了我来自 table_a 的所有记录,这些记录在 ids 列表中并且没有出现在 table_b.

select * from table_a 
where id in (1,2,3)
   and id not in 
      (select tablea_id from table_b 
       where tablea_id in (1,2,3))

那么在 eloquent 中我该怎么做呢?我想避免使用原始查询。

//does not work
TableA::whereIn('id',$ids)
   ->whereNotIn('id', TableB::select('tabla_id')->whereIn($ids));

要运行一个子查询你必须传递一个闭包:

TableA::whereIn('id',$ids)
      ->whereNotIn('id', function($q){
          $q->select('tabla_id')
            ->from('tableb');
            // more where conditions
      })
      ->get();