$employees = DB::table('users')
->leftJoin('assigned_branches','assigned_branches.user_id','=','users.id')
->leftJoin('assigned_geo_infos','assigned_geo_infos.id' ,'=','assigned_branches.region_branch_id')
->leftJoin('user_customers','user_customers.user_id','=','assigned_branches.user_id')
->leftJoin('customers','customers.id','=','user_customers.customer_id')
->whereIn('assigned_geo_infos.project_id',$assigned_projects_ids)
->where([['users.office_staff',0],['users.active',$filter]])
->select('assigned_geo_infos.*','assigned_geo_infos.id as info_id','assigned_geo_infos.name as info_name','assigned_branches.*','assigned_branches.level as region_branch_level','users.*','customers.customer_name')
->paginate(15);
因此,在这里我只想对用户表进行分页处理或计算,而不要对其他表进行分页计算。根据用户总数,应为2,但由于联接而导致总数为8。或者是否有其他解决方案我可以将所有联接表记录作为主表记录数组的子数组获取。
答案 0 :(得分:0)
使用groupBy()方法对结果进行分组:
$employees = DB::table('users')
->leftJoin('assigned_branches','assigned_branches.user_id','=','users.id')
->leftJoin('assigned_geo_infos','assigned_geo_infos.id' ,'=','assigned_branches.region_branch_id')
->leftJoin('user_customers','user_customers.user_id','=','assigned_branches.user_id')
->leftJoin('customers','customers.id','=','user_customers.customer_id')
->whereIn('assigned_geo_infos.project_id',$assigned_projects_ids)
->where([['users.office_staff',0],['users.active',$filter]])
->select('assigned_geo_infos.*','assigned_geo_infos.id as info_id','assigned_geo_infos.name as info_name','assigned_branches.*','assigned_branches.level as region_branch_level','users.*','customers.customer_name')
->groupBy('users.id')
->paginate(15);