public function search() { $model = Candidate::with('documents', 'documents.document_type', 'eligibility_form', 'office_location', 'RejectReason', 'TelephoneInterview', 'phases', 'f2fInterview', 'f2fInterviewForm', 'referee_form_filled', 'referee_form', 'title'); if (request('office_location_id')) { $model->whereHas('office_location', function ($q) { $q->whereIn('id', explode(",", request('office_location_id'))); }); } if (request('stage_id')) { $model->whereHas('stages', function ($q) { $q->whereIn('id', explode(",", request('stage_id'))); }); } if (request('experience_id')) { $model->whereHas('experience', function ($q) { $q->whereIn('job_type_id', explode(",", request('experience_id'))); }); } if (request('duration_id')) { $model->whereHas('experience', function ($q) { $q->where('duration_id', request('duration_id')); }); } if (request('references_filled')) { $model->whereHas('referee_form', function ($q) { $q->select('candidate_id') ->havingRaw('sum(filled)' . request('references_filled')) ->groupBy(['candidate_id']); }); } if (request('phase')) { $model->where('phase', request('phase')); } else { $model->where('phase', '!=', '4'); } if (request('has_car')) { $model->where('has_vehicle', '=', 1); } if (request('has_lisence')) { $model->where('can_drive', '=', 1); } if (request('has_dbs')) { $model->where('has_dbs', '=', 1); } if (request('min')) { $to = (request('min')); $from = (request('max')); $model->whereBetween(DB::raw('dob'), [DB::raw("CAST('" . strval($from) . "'" . ' AS DATE)'), DB::raw("CAST('" . strval($to) . "'" . ' AS DATE)')]); } if (request('regi_date')) { $model->where(DB::raw('DATE(created_at)'), request('regi_date')); } if (request('f2f_date')) { $model->whereHas('f2fInterview', function ($q) { $q->where(DB::raw('DATE(interview_time)'), request('f2f_date')); }); } $search_text = request('search_text'); if (request('search_text')) { $model->where(function ($q) use ($search_text) { $q->where('username', 'like', '%' . request('search_text') . '%'); $q->orWhere('first_name', 'like', '%' . request('search_text') . '%'); $q->orWhere('last_name', 'like', '%' . request('search_text') . '%'); $q->orWhere('email', 'like', '%' . request('search_text') . '%'); $q->orWhere('mobile_no', 'like', '%' . request('search_text') . '%'); }); } return response()->json(['data' => $model->paginate(25)]); }