/** * Update the specified vote type in storage. * * @param VoteTypeRequest $request * @param int $id The id of the vote type to update * @return Response */ public function update(VoteTypeRequest $request, $id) { // Find vote type, and update the title $vt = VoteType::findOrFail($id); $vt->title = $request->input('title'); $vt->save(); $prevAnswers = VoteTypeAnswer::where('type', '=', $vt->id)->get(); // Get previous answers of this vote type from the database $answers = $request->input('answers'); // Get new answers of this vote type from the form // Check if the answers are the same (if they are, only title changed) $same = true; if ($prevAnswers->count() == count($answers)) { // Count is the same, check each answer for ($i = 0; $i < $prevAnswers->count(); $i++) { if ($prevAnswers[$i]->answer != $answers[$i]) { $same = false; break; } } } else { $same = false; // If count is different, answers have changed } if (!$same) { // Delete all previous answers of this vote type (GroupVotes will be deleted too) foreach ($prevAnswers as $pa) { $pa->delete(); } // Add the new answers to the vote type foreach ($answers as $answer) { $this->saveVoteTypeAnswer($answer, $vt->id); } } // Redirect Session::flash('message', 'Ο τύπος απάντησης αποθηκεύτηκε με επιτυχία!'); return Redirect::to('votetypes'); }