2016-10-06 129 views
0

我想加盟laravel 5.3.I三個表要加入三個表並獲取審批的名字和部門名稱SQL連接三個表中laravel 5.3

table travel_request 
id 
approver_id (fk) 
department_id (FK) 

user table 
approver_id 
name 

Department table 
department_id 
dept_name 

query 
$users = DB::table('travel_request') 
     ->join('department', 'travel_request.department_id', '=', 'department.department_id') 
     ->join('users', 'travel_request.department_id', '=', 'users.approver_id') 
     ->select('travel_request.*', 'department.name as dept_name','users.name as approver_name')->get(); 

回答

2

您使用->join('users', 'travel_request.department_id', '=', 'users.approver_id')

它應該be

$users = DB::table('travel_request') 
    ->join('department', 'travel_request.department_id', '=', 'department.department_id') 
    ->join('users', 'travel_request.approver_id', '=', 'users.approver_id') 
    ->select('travel_request.*', 'department.name as dept_name','users.name as approver_name')->get();