$query = Student::from('student as s') ->join('user as u','u.object_id','=','s.id') //2020 年 2 月 28 日 08:12:41 新增添加下载证书次数 ->leftjoin('check_result as cr','s.id','cr.student_id') //->leftJoin('classroom_member as cm','cm.user_id','=','u.id') //->leftJoin('classroom as c',function($join){ ->join('classroom_member as cm','cm.user_id','=','u.id') ->join('classroom as c',function($join){ $join->on('c.id','=','cm.classroom_id') ->where('c.status','<>',2); }) ->when($year, function($query)use($year){ return $query->whereRaw('SUBSTRING(c.start_date,1,4) = '.$year); }) ->where('s.is_delete', 0) ->where('u.status',0) ->when(get_user_school_id(),function($query) { $query->where('c.school_id',get_user_school_id()); }) ->where('u.type',0) //学生 ->when($search, function($query)use($search){ return $query->where(function($q)use($search){ $q->where('s.name','like',$search.'%') ->orWhere('s.mobile','like',$search.'%'); }); }) ->orderBy('c.id','desc'); $peixunrenci = $query->distinct('cm.id')->count('cm.id'); $list = $query->groupBy('s.id') ->select( 's.id as stuid','s.name','s.sex','s.nation','s.mobile','s.id_card','s.work', 's.title','s.address', 's.email','s.office_phone','s.create_time','s.nation', 's.post_code', 'u.id as uid','cr.download_count' ) ->addSelect(DB::raw('count(distinct(cm.id)) as count, max(c.start_date) as date')) ->paginate($pageSize); 
