/** * Handle ajax, the function will be called twice * 1. For the first Ajax request, which further will invoke response for bodyonload (call the function again) * 2. Response to Ajax response by forwarding to actual view loaded with queried course_id * The reason for this two calling because in the first step, page is loaded asynchronously (not replaced in current page) * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function showTopicFiltered(Request $request) { $user = Auth::user(); //get the mapping for lecturer $kode_dosen = $user->lecturer->Kode_Dosen; //get Kode_Matkul & Nama_Matkul from course, therefore not Courses Model instance $courses = Course::courseMapByLecturerId($kode_dosen); //filter by code dosen $courses_arr = Helpers::toAssociativeArrays($courses); $course_id = $request['Kode_Matkul']; //need mapping from kode_matkul to kode_seksi $Topic = Topic::topicsByKodeMatkul($course_id); //filter by kode matkul registered, we do not required to filter by kode dosen since previous form already filtered it $input = $request->all(); //debugging purpose $response = array('response' => 'Called created successfully', '_token' => $input['_token'], 'Kode_Matkul' => $course_id); //technically only post can only enter this function if (!array_key_exists('step', $input)) { return response()->json($response); } return view('lecturers.showtopic')->with('Topic', $Topic)->with('Courses', $courses_arr)->with('course_id', $course_id); }