如何查询查询生成器laravel 5.4?
select * from gb_employee where employee_id not in (select gb_emp_client_empid from gb_emp_client_lines where gb_emp_client_clientid =1) ;
答案 0 :(得分:2)
为此使用左连接
$employee=DB::table('gb_employee as e')
->select('e.*')
->leftJoin('gb_emp_client_lines as el', 'e.employee_id', '=', 'el.gb_emp_client_empid')
->whereNull('el.gb_emp_client_empid',)
->get()
;
雄辩地做事
class Employee extends Model
{
public function client_lines()
{
return $this->hasMany('App\ClientLines', 'gb_emp_client_empid', 'employee_id');
}
}
$employees = Employee::doesntHave('client_lines')->get();
答案 1 :(得分:0)
我想我解决了。 。
$employees = DB::table('gb_employee')
->whereNotIn('employee_id', function($query) use ($client_id)
{
$query->select('gb_emp_client_empid')
->from('gb_emp_client_lines')
->where('gb_emp_client_clientid',$client_id);
})
->get();