public function postEdit(Request $request, $id) { $qcm = Qcm::where('id', $id)->with('user', 'questions')->first(); if (Auth::id() === $qcm->user->id) { $this->validate($request, ['name' => 'required|string', 'description' => 'required|string', 'subject_id' => 'required|in:' . implode(',', array_keys(Subject::toList())), 'questions' => 'required|array', 'valids_answers' => 'required|array|size_array:questions|answer_exists:answers', 'answers' => 'required|array']); $ret = DB::transaction(function () use($request, $qcm) { $datas = $request->all(); $questions = $qcm->questions; $qcm->name = $datas['name']; $qcm->description = $datas['description']; $qcm->subject_id = $datas['subject_id']; $qcm->update(); foreach ($questions as $q => $question) { $question->question = $datas['questions'][$q]; $question->update(); foreach ($question->answers as $a => $answer) { $answer->answer = $datas['answers'][$q][$a]; $answer->isValid = $datas['valids_answers'][$q] == $a; $answer->update(); } } return true; }); if ($ret) { Session::push('messages', 'success|Le QCM a bien été modifié'); } else { Session::push('messages', "danger|Le QCM n'a pas été modifié"); } return redirect(route('qcm::mine')); } else { Session::push('messages', 'danger|Vous ne pouvez pas modifier le QCM d\'un autre professeur'); Auth::logout(); return redirect(route('index')); } }