在laravel 5.3中的三个表的sql连接

时间:2016-10-06 09:12:42

标签: php mysql laravel

我想在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();

1 个答案:

答案 0 :(得分:2)

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

应该是

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