0

how can I make this query to query builder 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) ;
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Vinu Krishnan
  • 31
  • 1
  • 5
  • 3
    Possible duplicate of [Laravel Eloquent with two “WHERE NOT IN” in subquery](https://stackoverflow.com/questions/44743083/laravel-eloquent-with-two-where-not-in-in-subquery) – Jerodev Jun 20 '18 at 07:54

2 Answers2

2

Use a left join for this

$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()
;

doing via eloquent way

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();
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0

I think I solved it. . .

$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();
Vinu Krishnan
  • 31
  • 1
  • 5