简体   繁体   中英

How to store each answer in the answers table? (how to properly store in the array)

If the user selects quantity “1” for registration type general and “1” for registration type plus and click “Next” and goes to the registration form.

In the form, the user needs to introduce the name and surname for each participant. Then, the registration type general has 3 custom questions associated with it, so for the registration type general the user also need to answer to that 3 custom questions. The questions and the answers answered by the user in the registration form were:

Question                                  Answer
input text custom question                text answer
long text custom question:                long answer
checkbox custom question:                 check1

With the registration form as it is, and with the answers above, the participant array in the $request shows like below. The name, surname, regtypes is stored properly but the answers are not be stored properly in the array:

 "participant" => array:2 [▼
        1 => array:15 [▼
          "name" => "John"
          "surname" => "W"
          0 => array:1 [▼
            "answer" => "text answer"
          ]
          1 => array:1 [▼
            "question_id" => "1"
          ]
          2 => array:1 [▼
            "answer" => "long answer"
          ]
          3 => array:1 [▼
            "question_id" => "2"
          ]
          4 => array:1 [▼
            "answer" => "check1"
          ]
          5 => array:1 [▼
            "question_id" => "3"
          ]
          "regtypes" => "1"
        ]
        2 => array:3 [▼
          "name" => "Jake"
          "surname" => "K"
          "regtypes" => "4"
        ]
    ]

With the data stored like that using this foreach to insert the participants and the answers in the database shows "Undefined index: answer" in $participant['answer'] :

foreach ($participants_list as $participant) {

    $name = $participant['name'];
    $surname = $participant['surname'];

    $participant_result = Participant::create([
         'name' => $name,
         'surname' => $surname,
         'registration_type_id' => $participant['regtypes']
    ]);

    Answer::create([
        'participant_id' => $participant_result->id,
        'answer' => $participant['answer'],
        'question_id' => $participant['question_id']
    ]);
}

Do you know what can be the issue? Maybe is because how the answers and question_id are stored in the array but Im not understanding how to correct that properly.

Registration Form:

<form method="post" action="https://proj.test/conf/1/conf-test/registration/store">
  <h6>Participant - 1 - general</h6>

  <div class="form-group">
    <label for="namegeneral_1">Name</label>
    <input type="text" required id="namegeneral_1" name="participant[1][name]" class="form-control" value="">
  </div>

  <div class="form-group">
    <label for="surnamegeneral_1">Surname</label>
    <input type="text" required id="surnamegeneral_1" class="form-control" name="participant[1][surname]" value="">
  </div>

  <div class="form-group">
    <label for="participant_question">input text custom question</label>
    <input type='text' name='participant[1][][answer]' class='form-control' required>
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden"  value="1" name="participant[1][][question_id]"/>
  </div>

  <div class="form-group">
    <label for="participant_question">long text custom question</label>
    <textarea name='participant[1][][answer]' class='form-control' rows='3' required></textarea>
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="2" name="participant[1][][question_id]"/>
  </div>

  <div class="form-group">
    <label for="participant_question">checkbox custom question</label>
    <div class='checkbox-group required'> 
      <div class='form-check'>
        <input type='checkbox' name='participant[1][][answer]' value='select1' class='form-check-input' >
        <label class='form-check-label' for='exampleCheck1'>check1</label>
      </div> 
      <div class='form-check'>
        <input type='checkbox' name='participant[1][][answer]' value='select2' class='form-check-input' >
        <label class='form-check-label' for='exampleCheck1'>check2</label>
      </div>
    </div>
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="3" name="participant[1][][question_id]"/>
  </div>

  <input type="hidden" name="participant[1][regtypes]" value="1"/>

  <h6>Participant - 2 - plus</h6>

  <div class="form-group">
    <label for="nameplus_2">Name</label>
    <input type="text" required id="nameplus_2" name="participant[2][name]" class="form-control" value="">
  </div>

  <div class="form-group font-size-sm">
    <label for="surnameplus_2">Surname</label>
    <input type="text" required id="surnameplus_2" class="form-control" name="participant[2][surname]" value="">
  </div>

  <input type="hidden" name="participant[2][regtypes]" value="4"/>

  <input type="submit" class="btn btn-primary" value="Register"/>
</form>

In your loop, you do this:

foreach ($participants_list as $participant) {

So $participant is:

1 => array:15 [▼
          "name" => "John"
          "surname" => "W"

However answer key is in a sub array of the above, so from the above array it's the next level:

      0 => array:1 [▼
        "answer" => "text answer"

So answer key is not in $participant loop value array as per your array, in full:

 "participant" => array:2 [▼
        1 => array:15 [▼
          "name" => "John"
          "surname" => "W"
          0 => array:1 [▼
            "answer" => "text answer"

answer would be in $participant[0]['answer']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM