0

Help please understand how to save data of this kind: there is a site with a quiz, and the possibility of creating an unlimited number of questions

enter image description here

part of view create.blade.php:

<div class="form-group {{ $errors->has('question_title') ? 'has-error' : ''}}">
    <label for="question_title1" class="col-md-4 control-label">{{ 'question №1' }}</label>
    <div class="col-md-4">
<!--
        <input type="hidden" name="question_number[]" value="1"> 
-->
        <input class="form-control" name="question_title1" type="text" id="question_title" 
        value="" required>

    {!! $errors->first('question_title1', '<p class="help-block">:message</p>') !!}
    </div> <br>  <br>




A.  <input class="" name="question_description1[]" type="text" id="question_description1" value="a1"> 
    <input type="checkbox" name="question_answer1[]" value="1" checked> <br>

B. <input class="" name="question_description1[]" type="text" id="question_description1" value="a2"> 
    <input type="checkbox" name="question_answer1[]" value="2"> <br>

C. <input class="" name="question_description1[]" type="text" id="question_description1" value="a3"> 
    <input type="checkbox" name="question_answer1[]" value="3"> <br>

D. <input class="" name="question_description1[]" type="text" id="question_description1" value="a4"> 
    <input type="checkbox" name="question_answer1[]" value="4"> <br>    

</div>

part of controller QuizController.php:

   public function store(StoreQuizRequest $request)
    {

    $data_quiz = $request->only(
    'title', 'description', 'category', 'published',  'access', 'start_date', 'end_date',
    'duration','show_answers');

    $data_quiz['user_id'] = Auth::user()->id;

    $quiz = Quiz::create($data_quiz);




 $question_1['question_title']=$request->question_title1;
 $question_1['question_description']=implode('|&-&|',$request->question_description1);
 $question_1['question_answer']=implode('|',$request->question_answer1);
 $question_1['quiz_id'] = $quiz->id;


 $question_2['question_title']=$request->question_title2;
 $question_2['question_description']=implode('|&-&|',$request->question_description2);
 $question_2['question_answer']=implode('|',$request->question_answer2);
 $question_2['quiz_id'] = $quiz->id;



 $question_save1 = Question::create($question_1);
 $question_save2 = Question::create($question_2);

 return redirect('quiz')->with('flash_message', 'Quiz created!');


    }

How to create a loop in controller to save all questions with answers to DB? Thanks for answers!

2
  • Are questions are added dynamically (for example by admin panel)? Commented Dec 17, 2017 at 14:11
  • i plan to add add question dynamically in JS with adding new div blocks by clicking on button "add more question" Commented Dec 17, 2017 at 14:24

1 Answer 1

1

Let check it out: How do I create arrays in a HTML ?

You could create a form like this, and duplicate a form group if more questions are added:

<div class="form-group">
    ....
    <input name="question_title[]" />

    A. <input name="question_descriptionA[]" type="text" id="question_descriptionA" value="a1" />
    <input type="checkbox" name="question_answerA[]" value="1" checked /> <br />

    B. <input name="question_descriptionB[]" type="text" id="question_descriptionB" value="a2" />
    <input type="checkbox" name="question_answerB[]" value="2" /> <br />

    C. <input name="question_descriptionC[]" type="text" id="question_descriptionC" value="a3" />
    <input type="checkbox" name="question_answerC[]" value="3" /> <br />

    D. <input name="question_descriptionD[]" type="text" id="question_descriptionD" value="a4" />
    <input type="checkbox" name="question_answerD[]" value="4" /> <br />
    ....
</div>

And you can access the request data as array, as follows:

$data = $request->only('question_title',
    'question_descriptionA', 'question_answerA', 
    'question_descriptionB', 'question_answerB', 
    'question_descriptionC', 'question_answerC', 
    'question_descriptionD', 'question_answerD');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.