distinct() and count()

    static function exam_users($options, $return_type=0){
       $result = DB::table('Test_Survey_Log as a')
            ->join('Test_Users as b', 'b.id', '=', 'a.user_id')
            ->select(
                'b.*'
            )
            ->where('a.survey_id', $options['exam_id']);

        if($return_type == self::DT_TOTAL){
            return $result->count(DB::raw('DISTINCT b.id'));
        }

        $keyword = val($options, 'keyword');
        if($keyword != false){
            $result = $result->where(function($query) use ($keyword){
                $find = '%' . $keyword . '%';
                $query->orWhere('b.user_name', 'like', $find);
                $query->orWhere('b.gid', 'like', $find);
                $query->orWhere('b.english_name', 'like', $find);
                $query->orWhere('b.email', 'like', $find);
                $query->orWhere('b.company', 'like', $find);
            });
        }

        if($return_type == self::DT_FILTERED){
            return $result->count(DB::raw('DISTINCT b.id'));
        }

        if(val($options, 'sort') != false){
            $sorting = val($options, 'sort');
            foreach ($sorting as $key => $value) {
                $result = $result->orderBy($key, $value);
            }
        }else{
            $result = $result->orderBy('b.created_time', 'desc');
        }

        $result = $result->skip($options['start'])
            ->distinct('b.*')
            ->take($options['limit'])
            ->get();
        
        return $result;
    }