Fix: Сохранение картинок в ответах

 Исправлена проверка hasText/hasImage в контроллере
 answer_text сохраняется как null если пустой
 Картинки отображаются в edit.blade.php в input-group-text
 Обновлены store и update методы

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mirivlad 2026-03-27 11:48:59 +08:00
parent 8dc9e59a68
commit eefd5fc5da
2 changed files with 16 additions and 8 deletions

View File

@ -80,13 +80,16 @@ class QuestionController extends Controller
// Ответы для multiple_choice
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
foreach ($validated['answers'] as $answer) {
if (!empty($answer['text']) || !empty($answer['image'])) {
$hasText = !empty($answer['text']);
$hasImage = !empty($answer['image']);
if ($hasText || $hasImage) {
$imagePath = null;
if (!empty($answer['image'])) {
if ($hasImage) {
$imagePath = $answer['image']->store('questions/answers', 'public');
}
$question->answers()->create([
'answer_text' => $answer['text'],
'answer_text' => $hasText ? $answer['text'] : null,
'image' => $imagePath,
'is_correct' => $answer['is_correct'] ?? false,
'sort_order' => $answer['sort_order'] ?? 0,
@ -187,13 +190,16 @@ class QuestionController extends Controller
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
$question->answers()->delete();
foreach ($validated['answers'] as $answer) {
if (!empty($answer['text']) || !empty($answer['image'])) {
$hasText = !empty($answer['text']);
$hasImage = !empty($answer['image']);
if ($hasText || $hasImage) {
$imagePath = null;
if (!empty($answer['image'])) {
if ($hasImage) {
$imagePath = $answer['image']->store('questions/answers', 'public');
}
$question->answers()->create([
'answer_text' => $answer['text'],
'answer_text' => $hasText ? $answer['text'] : null,
'image' => $imagePath,
'is_correct' => $answer['is_correct'] ?? false,
'sort_order' => $answer['sort_order'] ?? 0,

View File

@ -40,10 +40,12 @@
<div id="answersContainer">
@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="text" name="answers[{{ $loop->index }}][text]" class="form-control" placeholder="Текст ответа" value="{{ $answer->answer_text ?? '' }}">
<input type="file" name="answers[{{ $loop->index }}][image]" class="form-control" accept="image/*">
@if($answer->image)
<img src="{{ asset('storage/' . $answer->image) }}" alt="Ответ" style="height:40px;">
<div class="input-group-text">
<img src="{{ asset('storage/' . $answer->image) }}" alt="Ответ" style="height:40px;object-fit:contain;">
</div>
@endif
<input type="hidden" name="answers[{{ $loop->index }}][is_correct]" value="{{ $answer->is_correct ? '1' : '0' }}">
<button type="button" class="btn {{ $answer->is_correct ? 'btn-success' : 'btn-outline-success' }}" onclick="toggleCorrect(this)">