/** * Store a user survey in storage. * * @param Request $request * @param Survey $survey * @return \Illuminate\Http\RedirectResponse */ public function userSurvey(Request $request, Survey $survey) { // add validation rules and error messages to insure all radio buttons of the survey are filled out $rules = []; $messages = []; for ($i = 1; $i <= $survey->size(); $i++) { $rules['radio.' . $i] = 'required'; $messages['radio.' . $i . '.required'] = 'Survey Question ' . $i . ' is required.'; } $this->validate($request, $rules, $messages); //store answers in database if all questions have been filled out $numResponses = count($request->input('radio')); for ($i = 1; $i <= $numResponses; $i++) { $answer = SurveyAnswer::findOrFail($request->input('radio.' . $i)); Auth::user()->surveys()->attach($survey->id, ['survey_question_id' => $answer->survey_question_id, 'survey_answer_id' => $answer->id]); } return back(); }