/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(Requests\Admin\Questions\Store $request, Question $Question) { $Question->fill($request->input('question')); $Question->save(); if ($request->hasFile('image') && $request->file('image')->isValid()) { $QuestionImage = $request->file('image'); $QuestionImage = $QuestionImage->move('upload\\questions', $Question->id . '.' . $QuestionImage->getClientOriginalExtension()); $Question->image = $QuestionImage->getPathname(); $Question->save(); } elseif ($request->has('question.delete_image') && $request->input('question.delete_image') == 'on') { $Question->image = null; $Question->save(); } $IdsAnswers = []; foreach ($request->input('answers') as $key => $answer) { $Answer = Answer::findOrNew($request->input('answers.' . $key . '.id')); $Answer->fill(['text' => $request->input('answers.' . $key . '.text'), 'right' => $request->input('answers.' . $key . '.right', false), 'question_id' => $Question->id]); $Answer->save(); $IdsAnswers[] = $Answer->id; if ($request->hasFile('answers.' . $key . '.image') && $request->file('answers.' . $key . '.image')->isValid()) { $AnswerFile = $request->file('answers.' . $key . '.image'); $AnswerFile = $AnswerFile->move('upload\\answers', $Answer->id . '.' . $AnswerFile->getClientOriginalExtension()); $Answer->image = $AnswerFile->getPathname(); $Answer->save(); } elseif ($request->has('answers.' . $key . '.delete_image') && $request->input('answers.' . $key . '.delete_image') == 'on') { $Answer->image = null; $Answer->save(); } } $AnswersForDeleting = $Question->answers()->whereNotIn('id', $IdsAnswers)->get(); if ($AnswersForDeleting->count()) { foreach ($AnswersForDeleting as $Answer) { $Answer->delete(); } } return redirect()->back(); }