diff --git a/app/Http/Controllers/Admin/QuestionController.php b/app/Http/Controllers/Admin/QuestionController.php index 9b38855..6a5946d 100755 --- a/app/Http/Controllers/Admin/QuestionController.php +++ b/app/Http/Controllers/Admin/QuestionController.php @@ -237,11 +237,22 @@ class QuestionController extends Controller $hasImage = $request->hasFile("answers.$index.image"); $isCorrect = isset($answer['is_correct']) && $answer['is_correct'] === '1'; - if ($hasText || $hasImage) { - $imagePath = null; - if ($hasImage) { - $imagePath = $request->file("answers.$index.image")->store('questions/answers', 'public'); + // Получаем существующую картинку если новая не загружена + $existingImagePath = $answer['existing_image'] ?? null; + $imagePath = null; + + if ($hasImage) { + // Удаляем старую если есть + if ($existingImagePath) { + \Storage::disk('public')->delete($existingImagePath); } + $imagePath = $request->file("answers.$index.image")->store('questions/answers', 'public'); + } else { + // Оставляем существующую + $imagePath = $existingImagePath; + } + + if ($hasText || $imagePath) { $question->answers()->create([ 'answer_text' => $hasText ? $answer['text'] : null, 'image' => $imagePath, diff --git a/resources/views/admin/questions/edit.blade.php b/resources/views/admin/questions/edit.blade.php index db908a7..9a7950a 100644 --- a/resources/views/admin/questions/edit.blade.php +++ b/resources/views/admin/questions/edit.blade.php @@ -41,6 +41,7 @@ @foreach($question->answers as $answer)