From 71cb31f784d4e8332c345e39a549b4662f6ff343 Mon Sep 17 00:00:00 2001 From: mirivlad Date: Fri, 27 Mar 2026 12:40:11 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20is=5Fcorrect=20=D0=B8=20=D0=BA=D0=B0?= =?UTF-8?q?=D1=80=D1=82=D0=B8=D0=BD=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Используем $request->input('answers') вместо $validated['answers'] ✅ is_correct проверяем как строку '1' ✅ Картинки загружаются через $request->file() ✅ Обновлены store и update методы Co-authored-by: Qwen-Coder --- .../Controllers/Admin/QuestionController.php | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Admin/QuestionController.php b/app/Http/Controllers/Admin/QuestionController.php index 79c342e..ab70763 100755 --- a/app/Http/Controllers/Admin/QuestionController.php +++ b/app/Http/Controllers/Admin/QuestionController.php @@ -48,13 +48,20 @@ class QuestionController extends Controller 'is_required' => 'boolean', 'answers' => 'nullable|array', 'answers.*.text' => 'nullable|string', - 'answers.*.image' => 'nullable|image|max:2048', 'matching_pairs' => 'nullable|array', 'ordering_items' => 'nullable|array', ]); - // Получаем картинки из request напрямую (не через validate) - $answersImages = $request->input('answers', []); + // Валидация картинок отдельно + if ($request->has('answers')) { + foreach ($request->input('answers', []) as $index => $answer) { + if ($request->hasFile("answers.$index.image")) { + $request->validate([ + "answers.$index.image" => 'image|max:2048', + ]); + } + } + } // Проверка что есть хотя бы текст или картинка в ответах if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) { @@ -82,10 +89,12 @@ class QuestionController extends Controller // Ответы для multiple_choice if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) { - foreach ($validated['answers'] as $index => $answer) { + foreach ($request->input('answers', []) as $index => $answer) { $hasText = !empty($answer['text']); $hasImage = $request->hasFile("answers.$index.image"); - + $isCorrect = isset($answer['is_correct']) && $answer['is_correct'] === '1'; + + // Сохраняем если есть текст или картинка if ($hasText || $hasImage) { $imagePath = null; if ($hasImage) { @@ -94,8 +103,8 @@ class QuestionController extends Controller $question->answers()->create([ 'answer_text' => $hasText ? $answer['text'] : null, 'image' => $imagePath, - 'is_correct' => $answer['is_correct'] ?? false, - 'sort_order' => $answer['sort_order'] ?? 0, + 'is_correct' => $isCorrect ? 1 : 0, + 'sort_order' => $answer['sort_order'] ?? $index, ]); } } @@ -192,10 +201,11 @@ class QuestionController extends Controller // Ответы if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) { $question->answers()->delete(); - foreach ($validated['answers'] as $index => $answer) { + foreach ($request->input('answers', []) as $index => $answer) { $hasText = !empty($answer['text']); $hasImage = $request->hasFile("answers.$index.image"); - + $isCorrect = isset($answer['is_correct']) && $answer['is_correct'] === '1'; + if ($hasText || $hasImage) { $imagePath = null; if ($hasImage) { @@ -204,8 +214,8 @@ class QuestionController extends Controller $question->answers()->create([ 'answer_text' => $hasText ? $answer['text'] : null, 'image' => $imagePath, - 'is_correct' => $answer['is_correct'] ?? false, - 'sort_order' => $answer['sort_order'] ?? 0, + 'is_correct' => $isCorrect ? 1 : 0, + 'sort_order' => $answer['sort_order'] ?? $index, ]); } }