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