I have a context where I have a form for a user do a registration in a congress. And there are two different contexts: when "all_participant" is "1" and when "all_participant" is "0" in the congresses table.
When all_participant is "1":
- if "all_participant" is "1" in the congress table, and the user is doing a registration in some ticket types and none ticket type have custom questions associated, in the registration form is necessary to collect only the name and surname about each participant that the user is registering
- if "all_participant" is "1" and the user is doing a registration in some ticket types that have custom questions associated is necessary to collect the answer to that custom questions for each participant that is being registered in that ticket types that have that custom questions
To better when "all_participants" is 1 in the congress table:
When the user click "Go to step 2" in the registration form, is inserted an entry in the Registrations table, an entry for each participant in Participants table and entries in the answers table relative to the answers to the custom questions. So the database stays like below when the user click in "Go to step 2" in the registration form:
Registrations table:
id status congress_id main_participant_id
7 C 1 1
Participants table:
id registration_id ticket_type_id name surname
12 7 1 John W
13 7 2 Jake Y
Answers table:
id participant_id question_id answer
2 12 1 0002
3 13 1 0003
When all_participant is "0":
My doubt is about how to store the information when "all_participants" is "0". So the user John that is doing the registration selected 2 tickets, one ticket of the ticket type "tt1" and other of the ticket type "tt2", and the ticket type "tt1" has 1 custom question associated and now "all_participants" is "0", which means that is not necessary to collect info about each participant, is only necessary to use the info of the auth user to do the registration.
But if there are custom questions is necessary that the auth user (the user that is doing the registration) answer these custom questions, but if "all_participant" is "0" only the user that is doing the registration needs to answer these questions, so for example if the user selected two tickets and 1 or more have some custom questions associated, in the registration form, besides the user selected 2 tickets in the registration form it will only appear once the custom question(s) and not twice because is only for the user that is doing the registration to answer (because "all_participants" is "0"). So in this scenario when user clicks in the "Go to step 2" in the registration form the database stays like:
Registrations table:
id status congress_id main_participant_id
10 C 1 1
Participants table: (name and surname and blank because when "all_participant" is "0" is not necessary to collect name and surname of each participant)
id registration_id ticket_type_id name surname
18 10 1
19 10 2
Answers table:
id participant_id question_id answer
4 18 1 0002
Doubt:
My doubt is if you know if this is structured correctly because as it is it seems that is not possible to know to which user the answers belong when "all_participant" is "0" and there are custom questions in 1 or more ticket types selected by the user. Because the ansers table only have the participant_id which in this case is "18" but the user that did the registration is the user in the users table with id "1".
The main_participant_id in the registrations table is the id of the user in the users table that did the registration which allow to know which user did the registration.
To better illustrate the 3 possible scenarios when "all_participant" is 0:
Relationships relevant for the question:
1 to many between Congresses and Registrations
1 to many between Congresses and TicketTypes
1 to many between Registrations and Participants
1 to many between TicketTypes and Participants
1 to many between Participants and Answers
1 to many between Questions and Answers
Many to Many between TicketTypes and Questions
1 to many between Congresses and Questions
Relevant models for the question:
// Congress model
class Congress extends Model
{
// A conference has one creator
public function creator(){
return $this->belongsTo('App\User', 'user_id');
}
public function ticketTypes(){
return $this->hasMany('App\TicketType', 'congress_id');
}
public function registrations(){
return $this->hasMany('App\Registration', 'congress_id');
}
}
// User model
class User extends Authenticatable
{
public function congresses(){
return $this->hasMany('App\Congress', 'user_id');
}
// A user can register in many conferences
public function registrations(){
return $this->hasMany('App\Registration','main_participant_id');
}
}
// Registration model
class Registration extends Model
{
// a registration has one user that do the registration
public function customer(){
return $this->belongsTo('App\User');
}
// a registration can have many participants
public function participants(){
return $this->hasMany('App\Participant');
}
public function congress(){
return $this->belongsTo('App\Congress');
}
}
// Participant Model
class Participant extends Model
{
// a participant belongs to a registration
public function registration(){
return $this->belongsTo('App\Registration');
}
}
// Ticket Type model
class TicketType extends Model
{
public function congress(){
return $this->belongsTo('App\Congress');
}
public function questions(){
return $this->belongsToMany('App\Question', 'ticket_type_questions')->withPivot(['required']);;
}
}
// Question model
class Question extends Model
{
public function ticket_type(){
return $this->belongsToMany('App\TicketType', 'ticket_type_questions')
->withPivot('required');
}
}
// Answer model
class Answer extends Model
{
public function question(){
return $this->belongsTo('Question');
}
public function participant(){
return $this->belongsTo('Participant');
}
}
// TicketTypeQuestion model
class RegistrationTypeQuestion extends Model
{
}
To register the user and other participants that he can register, in both scenarios: "all_participant" as "1" and "all_participant" as "0", I have the register() method for now like :
public function register(Request $request, $id, $slug = null, Validator $validator){
$allParticipants = Congress::where('id', $id)->first()->all_participants;
$user = Auth::user();
$rules = [];
$messages = [];
if(isset($request->participant_question_required)) {
$messages = [
'participant_question.*.required' => 'Fill all mandatory fields',
'participant_name.*.required' => 'Fill all name fields.',
'participant_surname.*.required' => 'Fill all surname fields.',
];
foreach ($request->participant_question_required as $key => $value) {
$rule = 'string|max:255';
if ($value) {
$rule = 'required|' . $rule;
}
$rules["participant_question.{$key}"] = $rule;
}
}
if($allParticipants){
$rules["participant_name.*"] = 'required|max:255|string';
$rules["participant_surname.*"] = 'required|max:255|string';
}
$validator = Validator::make($request->all(), $rules, $messages);
$errors = $validator->errors();
$errors = json_decode($errors);
if($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $errors
], 422);
}
if($validator->passes()) {
$registration = Registration::create([
'congress_id' => $id,
'main_participant_id' => $user->id,
'status' => 'C',
]);
$participants = [];
for ($i = 0; $i < count($request->participant_name); $i++) {
$name = ($allParticipants) ? $request->participant_name[$i] : '';
$surname = ($allParticipants) ? $request->participant_surname[$i] : '';
$participants[] = Participant::create([
'name' => $name,
'surname' => $surname,
'registration_id' => $registration->id,
'ticket_type_id' => $request->ttypes[$i]
]);
}
if (isset($request->participant_question))
for ($i = 0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
}
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}