public function result(Request $req) { $input = $req->all(); if (isset($input['option'])) { $array_of_options = $input['option']; foreach ($array_of_options as $key => $value) { //$key = question id //$value = user submitted answer $answer = Answers::select('option_id')->where('question_id', '=', $key)->get(); if (count($answer) === 1) { //Single answer $answer = $answer->first(); if ($answer->option_id === $value) { //User answer is correct $correct_answer[$key] = $value; } else { //User answer isn't correct $wrong_answer[$key] = $value; } } else { //Multiple answer foreach ($answer as $ans) { foreach ($value as $val) { if ($ans->option_id === $val) { $multiple_right_answer[] = $val; } } } if (isset($multiple_right_answer)) { if (count($multiple_right_answer) === count($answer)) { $correct_answer[$key] = $value; } else { $wrong_answer[$key] = $value; } } else { $wrong_answer[$key] = $value; } } //End of Multiple answer $multiple_right_answer = null; } if (isset($correct_answer)) { $correct_answer_count = count($correct_answer); //Get the skill result $correct_answer_array = array_keys($correct_answer); $chart = $this->getSkillResult($correct_answer_array); $this->generatePdf($chart); } else { $correct_answer_count = 0; $correct_answer = null; $chart = null; } if (isset($wrong_answer)) { $wrong_answer_count = count($wrong_answer); } else { $wrong_answer_count = 0; $wrong_answer = null; } $success_percentage = $correct_answer_count * 100 / ($correct_answer_count + $wrong_answer_count); $result_data = ['user_id' => \Auth::user()->id, 'quiz_id' => $req->input('quiz_id'), 'total_attempt' => $correct_answer_count + $wrong_answer_count, 'correct_answers' => $correct_answer_count, 'percentage' => $success_percentage]; \DB::table('results')->insert($result_data); $user_given_inputs = $input['option']; //Call the pdf creation and sending email function here to include the $skill data and user result data return view('result')->with(['chart' => $chart, 'user_given_inputs' => $user_given_inputs, 'percentage' => $success_percentage, 'correct_answer' => $correct_answer, 'wrong_answer' => $wrong_answer]); } else { return view('result')->with(['message' => 'You did not answer any question. Try again!']); } }