Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spAo's avatar
Level 1

Quiz correct answer saving problem

Hello, i'm trying to save quiz correct answer but it's not working without correct answer it's works...

  public function store(Quizze $quizzes)
    {
        $data = request()->validate([
            'title.title' => 'required',
            'questions.*.question' => 'required',
            'questions.*.answers.*.answer' => 'required',
            'questions.*.answers.*.correct' => 'required',
        ], [
            'title.title.required' => 'გთხოვთ, შეიყვანოთ ქვიზის სახელი.',
            'questions.*.question.required' => 'გთხოვთ, შეიყვანოთ შეკითხვა.',
            'questions.*.answers.*.answer.required' => 'გთხოვთ, შეიყვანოთ პასუხი.',
            'questions.*.answers.*.correct.required' => 'error.',
        ]);
        dd($data);
        $storeQuiz = $quizzes->create($data['title']);
        foreach ($data['questions'] as $key => $q) {
            $question = $storeQuiz->questions()->create(['question' => $q['question']]);
            $question->answers()->createMany($data['questions'][$key]['answers']);
        }
        return redirect('admin/quizzes');
    }
 <form action="/admin/quiz/store" method="POST">
                        @csrf
                        <div class="form-group">
                            <label for="title">ქვიზის დასახელება</label>
                            <input name="title[title]" type="text" class="form-control" value="{{ old('title.title') }}" id="title" placeholder="">
                            @error('title.title')
                            <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="quiz-add-wrapper">
                            <div class="quiz-question-awnser-wrapper">
                                <div class="form-group d-flex align-items-end">
                                    <div class="quiz-question-input">
                                        <label for="">ქვიზის შეკითხვა</label>
                                        <input name="questions[1][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" quizquestion_id="1" placeholder="">
                                    </div>
                                    <!-- <span class="quiz-add-question font-size-30">+</span> -->
                                    <!-- <span class="quiz-remove-question font-size-30">-</span> -->
                                    @error('questions.0.question')
                                    <small class="text-danger">{{ $message }}</small>
                                    @enderror
                                </div>
                                <div class="form-group">
                                    <fieldset class="quiz-awnser-wrapper">
                                        <div class="form-group d-flex align-items-end">
                                            <div class="quiz-awnser-input">
                                                <label for="">ქვიზის პასუხი</label>
                                                <input type="radio" name="questions[1][answers][1][correct]" >
                                                <input name="questions[1][answers][1][answer]" type="text" class="form-control" value="{{ old('questions.1.answers.0.answer') }}" quizanswer_id="1" aria-describedby="choicesHelp" placeholder="">
                                            </div>
                                            <span class="quiz-add-answer font-size-30">+</span>
                                            <!-- <span class="quiz-remove-answer font-size-30">-</span> -->
                                            @error('questions.1.answers.0.answer')
                                            <small class="text-danger">{{ $message }}</small>
                                            @enderror
                                        </div>
                                    </fieldset>
                                </div>
                            </div>
                        </div>
                        <button type="submit" class="btn btn-primary">ქვიზის დამატება</button>
                        <button type="button" class='quiz-add-question-btn btn btn-success'>ქვიზის შეკითხვის დამატება</button>
                    </form>

What i'm doing wrong?

0 likes
6 replies
Tray2's avatar

What error message do you get?

spAo's avatar
Level 1

@Tray2 I'm getting nothing on dd($data); it's just redirects me to all quizzes page

spAo's avatar
Level 1

@Tray2 thanks for answer i fixed it. this question has nothing to do with this issue but i'm trying to delete quiz with questions and answers but i can't get to answers. how can i get answers?

   public function destroy(Quizze $quiz)
    {
        $quiz->questions->answers()->delete();
        $quiz->questions()->delete();
        $quiz->delete();
        return redirect()->back();
    }

Please or to participate in this conversation.