Fix: Сохранение is_correct и картинок
✅ Используем $request->input('answers') вместо $validated['answers'] ✅ is_correct проверяем как строку '1' ✅ Картинки загружаются через $request->file() ✅ Обновлены store и update методы Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
729e39bd78
commit
71cb31f784
|
|
@ -48,13 +48,20 @@ class QuestionController extends Controller
|
||||||
'is_required' => 'boolean',
|
'is_required' => 'boolean',
|
||||||
'answers' => 'nullable|array',
|
'answers' => 'nullable|array',
|
||||||
'answers.*.text' => 'nullable|string',
|
'answers.*.text' => 'nullable|string',
|
||||||
'answers.*.image' => 'nullable|image|max:2048',
|
|
||||||
'matching_pairs' => 'nullable|array',
|
'matching_pairs' => 'nullable|array',
|
||||||
'ordering_items' => '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'])) {
|
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
||||||
|
|
@ -82,10 +89,12 @@ class QuestionController extends Controller
|
||||||
|
|
||||||
// Ответы для multiple_choice
|
// Ответы для multiple_choice
|
||||||
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
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']);
|
$hasText = !empty($answer['text']);
|
||||||
$hasImage = $request->hasFile("answers.$index.image");
|
$hasImage = $request->hasFile("answers.$index.image");
|
||||||
|
$isCorrect = isset($answer['is_correct']) && $answer['is_correct'] === '1';
|
||||||
|
|
||||||
|
// Сохраняем если есть текст или картинка
|
||||||
if ($hasText || $hasImage) {
|
if ($hasText || $hasImage) {
|
||||||
$imagePath = null;
|
$imagePath = null;
|
||||||
if ($hasImage) {
|
if ($hasImage) {
|
||||||
|
|
@ -94,8 +103,8 @@ class QuestionController extends Controller
|
||||||
$question->answers()->create([
|
$question->answers()->create([
|
||||||
'answer_text' => $hasText ? $answer['text'] : null,
|
'answer_text' => $hasText ? $answer['text'] : null,
|
||||||
'image' => $imagePath,
|
'image' => $imagePath,
|
||||||
'is_correct' => $answer['is_correct'] ?? false,
|
'is_correct' => $isCorrect ? 1 : 0,
|
||||||
'sort_order' => $answer['sort_order'] ?? 0,
|
'sort_order' => $answer['sort_order'] ?? $index,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -192,10 +201,11 @@ class QuestionController extends Controller
|
||||||
// Ответы
|
// Ответы
|
||||||
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
||||||
$question->answers()->delete();
|
$question->answers()->delete();
|
||||||
foreach ($validated['answers'] as $index => $answer) {
|
foreach ($request->input('answers', []) as $index => $answer) {
|
||||||
$hasText = !empty($answer['text']);
|
$hasText = !empty($answer['text']);
|
||||||
$hasImage = $request->hasFile("answers.$index.image");
|
$hasImage = $request->hasFile("answers.$index.image");
|
||||||
|
$isCorrect = isset($answer['is_correct']) && $answer['is_correct'] === '1';
|
||||||
|
|
||||||
if ($hasText || $hasImage) {
|
if ($hasText || $hasImage) {
|
||||||
$imagePath = null;
|
$imagePath = null;
|
||||||
if ($hasImage) {
|
if ($hasImage) {
|
||||||
|
|
@ -204,8 +214,8 @@ class QuestionController extends Controller
|
||||||
$question->answers()->create([
|
$question->answers()->create([
|
||||||
'answer_text' => $hasText ? $answer['text'] : null,
|
'answer_text' => $hasText ? $answer['text'] : null,
|
||||||
'image' => $imagePath,
|
'image' => $imagePath,
|
||||||
'is_correct' => $answer['is_correct'] ?? false,
|
'is_correct' => $isCorrect ? 1 : 0,
|
||||||
'sort_order' => $answer['sort_order'] ?? 0,
|
'sort_order' => $answer['sort_order'] ?? $index,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue