I have a model “Conference” and a conference can have one or more registration types. For example the conference “conference test” has two registration types associated: the registration type “general” and the registration type “plus”. The user that created the conference can craete how many registration types he wants for the conference that he has createad. And for each registration type he can create custom questions, for example “Whats your phone?”.
In the case of the conference “conference test”, the registration type “general” has 2 custom questions associated and the registration type “plus” dont have any custom question associated.
If a user is doing a registration in the conference “conference test” and selects that he wants quantity “2” for the registration type “plus” and click “Next” the user is redirected to the registration page. In this page there is the registration form that in this case it will only ask for the name and surname of both participants, so the form is like: https://ibb.co/bJFsoK
When the user clicks in “Next” the $request data is like:
array:4 [▼
"participant" => array:2 [▼
1 => array:3 [▼
"name" => "John"
"surname" => "K"
"rtypes" => "2"
]
2 => array:3 [▼
"name" => "Jake"
"surname" => "W"
"rtypes" => "2"
]
]
]
And the information is stored with success in the database using the storeRegistration().
Doubt:
My doubt is when the user is doing a registration and selected a registration type(s) that has custom questions associated.
For example if instaed of the above scenario, the user selects the quantity “2” for the registration type “general” and “0” for the registration type “plus” and click “Next”, the registration form besides the name and surname will also ask for the “Phone” because the user selected quantity “2” for the registration type “general” that has a custom question associated (Phone). So the registration form will be like:
My doubt is how we can store, besides the name and surname of each participant, the answers to the custom questions so that is possible to store in the answers table the answers of each participant.
Do you know how to properly achieve that?
Registration form in the case of the user select the quantity “2” for the registration type “general” and “0” for “plus”:
<form method="post"
action="https://proj.test/conference/1/conference-test/registration/storeRegistration">
<h6>Participant - 1 - general</h6>
<div class="form-group font-size-sm">
<label for="namegeneral_1"
class="text-gray">Name</label>
<input type="text" required id="namegeneral_1"
name="participant[name]"
class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnamegeneral_1"
class="text-gray">Surname</label>
<input type="text" required id="surnamegeneral_1"
class="form-control"
name="participant[surname]" value="">
</div>
<div class="form-group">
<label for="participant_question">Phone</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>
<input type="hidden" name="participant[1][rtypes]" value="1"/>
<h6>Participant - 2 - general</h6>
<div class="form-group font-size-sm">
<label for="namegeneral_2"
class="text-gray">Name</label>
<input type="text" required id="namegeneral_2"
name="participant[name]"
class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnamegeneral_2"
class="text-gray">Surname</label>
<input type="text" required id="surnamegeneral_2"
class="form-control"
name="participant[surname]" value="">
</div>
<div class="form-group">
<label for="participant_question">Phone</label>
<input type='text' name='participant[2][answer]' class='form-control' required>
<input type="hidden"
name="participant_question_required[]"
value="1">
<input type="hidden"
value="1"
name="participant[2][question_id]"/>
</div>
<input type="hidden" name="participant[2][rtypes]" value="1"/>
<input type="submit" class="btn btn-primary" value="Register"/>
</form>
Método storeRegistration() to store the registration info:
public function storeRegistration(Request $request, $id, $slug = null)
{
# user object
$user = Auth::user();
# add registration to Database
$registration = Registration::create([
'congress_id' => $id,
'user_that_did_registration' => $user->id,
]);
# List of all participants
$participants_list = $request->get('participant');
#add all participants to Database
foreach ($participants_list as $participant) {
$name = $participant['name'];
$surname = $participant['surname'];
$participant_result = Participant::create([
'name' => $name,
'surname' => $surname,
'registration_id' => $registration->id,
'registration_type_id' => $participant['rtypes']
]);
# save answer to Database if exist
if (isset($participant['question_id'])) {
$answer = Answer::create([
'question_id' => $participant['question_id'],
'participant_id' => $participant_result->id,
'answer' => $participant['answer'],
]);
}
}
Session::flash('registration_success', 'Registration concluded.');
return redirect(route('user.index', ['user' => Auth::id()]) . '#myTickets');
}