Fix: Сохранение существующих картинок при редактировании

 edit.blade.php передаёт existing_image
 update() сохраняет existing_image если новый файл не загружен
 update() удаляет старую картинку если загружена новая

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mirivlad 2026-03-27 16:33:14 +08:00
parent 523e8912af
commit d1d7eecb69
2 changed files with 16 additions and 4 deletions

View File

@ -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,

View File

@ -41,6 +41,7 @@
@foreach($question->answers as $answer)
<div class="input-group mb-2">
<input type="text" name="answers[{{ $loop->index }}][text]" class="form-control" placeholder="Текст ответа" value="{{ $answer->answer_text ?? '' }}">
<input type="hidden" name="answers[{{ $loop->index }}][existing_image]" value="{{ $answer->image }}">
<input type="file" name="answers[{{ $loop->index }}][image]" class="form-control" accept="image/*">
@if($answer->image)
<div class="input-group-text">