如何使仅主表记录上的laravel 5.2分页工作(计数)不包括联接表

时间:2018-06-27 12:23:32

标签: laravel laravel-5.2

$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。或者是否有其他解决方案我可以将所有联接表记录作为主表记录数组的子数组获取。

1 个答案:

答案 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);