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

spAo's avatar
Level 1

Quiz is saving twice

Hello, i'm trying to save quiz tilte questions and answers. It's working fine in one question but in second question it's bugging

 <form action="/admin/quiz/store" method="POST">
                        @csrf
                        <div class="form-group">
                            <label for="title">quiz name</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="form-group">
                            <label for="question1">quiz question</label>
                            <input name="questions[][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question1" placeholder="">
                            @error('questions.0.question')
                            <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>
                        <div class="form-group">
                            <fieldset>
                                <div>
                                    <div class="form-group">
                                        <label for="answer1">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.0.answer') }}" id="answer1" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.0.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="answer2">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.1.answer') }}" id="answer2" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.1.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="answer3">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.2.answer') }}" id="answer3" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.2.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="answer4">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.3.answer') }}" id="answer4" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.3.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                               
                            </fieldset>
                        </div>
                       SECOND QUESTION
                        <div class="form-group">
                            <label for="question1">quiz question</label>
                            <input name="questions[][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question1" placeholder="">
                            @error('questions.0.question')
                            <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>
                        <div class="form-group">
                            <fieldset>
                                <div>
                                    <div class="form-group">
                                        <label for="answer1">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.0.answer') }}" id="answer1" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.0.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="answer2">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.1.answer') }}" id="answer2" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.1.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="answer3">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.2.answer') }}" id="answer3" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.2.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="answer4">quiz answer</label>
                                        <input name="answers[][answer]" type="text" class="form-control" value="{{ old('answers.3.answer') }}" id="answer4" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.3.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                               
                            </fieldset>
                        </div>
                        <button type="submit" class="btn btn-primary">add quiz</button>
                    </form>
    public function store(Quizze $quizzes)
    {
        $data = request()->validate([
            'title.title' => 'required',
            'questions.*.question' => 'required',
            'answers.*.answer' => 'required',
        ], [
            'title.title.required' => 'error.',
            'questions.*.question.required' => 'error.',
            'answers.*.answer.required' => 'error.'
        ]);
        $storeQuiz = $quizzes->create($data['title']);
        foreach ($data['questions'] as $q) {
            $question = $storeQuiz->questions()->create($q);
            $question->answers()->createMany($data['answers']);
        }
        return redirect('admin/quizzes');
    }

it's saving quiz title and questions normal but in answers table it's saving first question answers in question_id=1 and question_id=2 and it's saves second question same. sorry for my bad english (

0 likes
4 replies
tykus's avatar

You need to organize you form inputs properly. The answers input should really be scoped by its corresponding question rather that a global answers array, e.g.

<div>
    <label for="question1">Question</label><input id="question1" type="text" name="questions[1][question]">
    <label for="question1answer1">Question</label><input id="question1answer1" type="text" name="questions[1][answers][]">
    <label for="question1answer2">Question</label><input id="question1answer2" type="text" name="questions[1][answers][]">
    <label for="question1answer3">Question</label><input id="question1answer3" type="text" name="questions[1][answers][]">
</div>
<div>
    <label for="question2">Question</label><input id="question2" type="text" name="questions[2][question]">
    <label for="question2answer1">Question</label><input id="question2answer1" type="text" name="questions[2][answers][]">
    <label for="question2answer2">Question</label><input id="question2answer2" type="text" name="questions[2][answers][]">
    <label for="question2answer3">Question</label><input id="question2answer3" type="text" name="questions[2][answers][]">
</div>

Then your Request is structured; and easier to iterate over:

  "questions" => array:2 [▼
    1 => array:2 [▼
      "question" => "foo"
      "answers" => array:3 [▼
        0 => "foo1"
        1 => "foo2"
        2 => "foo3"
      ]
    ]
    2 => array:2 [▼
      "question" => "bar"
      "answers" => array:3 [▼
        0 => "bar1"
        1 => "bar2"
        2 => "bar3"
      ]
    ]
  ]
]
spAo's avatar
Level 1

@tykus i did like this

   <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="form-group">
                            <label for="question2">ქვიზის შეკითხვა</label>
                            <input name="questions[1][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question1" placeholder="">
                            @error('questions.0.question')
                            <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>
                        <div class="form-group">
                            <fieldset>
                                <div>
                                    <div class="form-group">
                                        <label for="question1answer1">ქვიზის პასუხი</label>
                                        <input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.0.answer') }}" id="question1answer1" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.0.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="question1answer2">ქვიზის პასუხი</label>
                                        <input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.1.answer') }}" id="question1answer2" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.1.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="question1answer3">ქვიზის პასუხი</label>
                                        <input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.2.answer') }}" id="question1answer3" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.2.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="question1answer4">ქვიზის პასუხი</label>
                                        <input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.3.answer') }}" id="question1answer4" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.3.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                               
                            </fieldset>
                        </div>
                        meore shekitxva
                        <div class="form-group">
                            <label for="question2">ქვიზის შეკითხვა</label>
                            <input name="questions[2][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question2" placeholder="">
                            @error('questions.0.question')
                            <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>
                        <div class="form-group">
                            <fieldset>
                                <div>
                                    <div class="form-group">
                                        <label for="question2answer1">ქვიზის პასუხი</label>
                                        <input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.0.answer') }}" id="question2answer1" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.0.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="question2answer2">ქვიზის პასუხი</label>
                                        <input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.1.answer') }}" id="question2answer2" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.1.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="question2answer3">ქვიზის პასუხი</label>
                                        <input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.2.answer') }}" id="question2answer3" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.2.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="form-group">
                                        <label for="question2answer4">ქვიზის პასუხი</label>
                                        <input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.3.answer') }}" id="question2answer4" aria-describedby="choicesHelp" placeholder="">
                                        @error('answers.3.answer')
                                        <small class="text-danger">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                               
                            </fieldset>
                        </div>
                        <button type="submit" class="btn btn-primary">ქვიზის დამატება</button>
                    </form>

It's errors Undefined array key "answers" i'm new in this so i don't really know if i'm doing it the right way...

tykus's avatar
tykus
Best Answer
Level 104

@spAo in the Controller, something like this...

$storeQuiz = $quizzes->create($data['title']);
foreach (request('questions') as $q) {
    $question = $storeQuiz->questions()->create(['column_name' => $q['question']);
    $question->answers()
        ->createMany(collect($q['answers'])->mapWithKeys(fn ($answer) => ['column_name' => $answer]));
}

Note that the column_name keys in the Question and Answer models should be modified appropriately for those Models

Tray2's avatar

Of course it does.

When you loop through the each question it insert all the answers every time.

$question->answers()->createMany($data['answers']);

You need to distinguish which answers belong to which question.

Please or to participate in this conversation.