title: How to properly store the necessary info to register the user and other participants in the congress? (scenario 1 works but scenario 2 and 3 dont)
body: <p>I have a registration form below for a user register in a congress. There are 4 different scenarios for the registration. So the registration form needs to handle this 4 different scenarios. But its not working, only the scenario 1 is working fine.</p>
  
  <p>Do you know how to achieve this scenarios?</p>
  
  <p><strong>Scenario 1: (the only scenario that is working fine)</strong></p>
  
  <ul>
  <li>there are two ticket types for the congress "ticket type 1" and "ticket type 2"</li>
  <li>the ticket type "tt1" has a custom question associate "Whats your phone?", the ticket type "tt2" dont have any custom question associated</li>
  <li>all participants is "1" which means that is necessary to collect info (name and surname) of each participant</li>
  <li>Diagram for this scenario:
  <a href="https://i.stack.imgur.com/l18tN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l18tN.png" alt="enter image description here"></a></li>
  </ul>
  
  <p><strong>Scenario 2:</strong></p>
  
  <ul>
  <li>there are two ticket types for the congress "tt1" and "tt2"</li>
  <li>the ticket types "tt1" and "tt2" dont have any custom question associated</li>
  <li>all participants is "1" which means that is necessary to collect info of each participant</li>
  <li>Diagram for this scenario: </li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/7dn8M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7dn8M.png" alt="enter image description here"></a></p>
  
  <p><strong>Scenario 3:</strong></p>
  
  <ul>
  <li>there are two ticket types for the congress "tt1" and "tt2"</li>
  <li>the ticket type "tt1" has a custom question associate "Whats your phone?", the ticket type "tt2" dont have any custom question associated</li>
  <li>all participants is "0" which means that is not necessary to collect info (name and surname) of each participant</li>
  <li>Diagram for this scenario: 
  <a href="https://i.stack.imgur.com/IwxHV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IwxHV.png" alt="enter image description here"></a></li>
  </ul>
  
  <p><strong>Scenario 4:</strong></p>
  
  <ul>
  <li>there are two ticket types for the congress "tt1" and "tt2"</li>
  <li>the ticket types "tt1" and "tt2" dont have any custom question associated</li>
  <li>all participants is "0" which means that is not necessary to collect info (name and surname) of each participant</li>
  <li>Diagram for this scenario: 
  <a href="https://i.stack.imgur.com/2E7oi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2E7oi.png" alt="enter image description here"></a></li>
  </ul>
  
  <p>The selected tickets from the previous page are available in the variable "<code>$selectedTypes</code>"</p>
  
  <p>// registration form in the registration.blade.php page</p>
  
  <pre><code>      &lt;form method="post" id="step1form" action=""&gt;
      {{csrf_field()}}
      @if (!empty($allParticipants))
          @if($allParticipants == 1)
              &lt;p&gt;Please fill in all fields. Your tickets will be sent to
                  p{{ (\Auth::check()) ? Auth::user()-&gt;email : old('email')}}.&lt;/p&gt;
  
              @foreach($selectedTypes as $k =&gt; $selectedType)
                  @foreach(range(1,$selectedType['quantity']) as $val)
  
                      &lt;h6&gt;Participant - {{$val}}  - {{$k}}&lt;/h6&gt;
                      &lt;div class="form-check"&gt;
                          &lt;input class="form-check-input" type="radio" name="payment_method" value="referencias"&gt;
                          &lt;label class="form-check-label d-flex align-items-center" for="exampleRadios1"&gt;
                              &lt;span class="mr-auto"&gt;Fill the following fields with the authenticated user information.&lt;/span&gt;
                          &lt;/label&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group font-size-sm"&gt;
                          &lt;label for="participant_name" class="text-gray"&gt;Name&lt;/label&gt;
                          &lt;input type="text" name="participant_name[]" required class="form-control" value=""&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group font-size-sm"&gt;
                          &lt;label for="participant_surname" class="text-gray"&gt;Surname&lt;/label&gt;
                          &lt;input type="text" required class="form-control" name="participant_surname[]" value=""&gt;
                      &lt;/div&gt;
                      &lt;input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/&gt;
                      @foreach($selectedType['questions'] as $customQuestion)
                          &lt;div class="form-group"&gt;
                              &lt;label for="participant_question"&gt;{{$customQuestion-&gt;question}}&lt;/label&gt;
                              &lt;input type="text"
                                     @if($customQuestion-&gt;pivot-&gt;required == "1") required @endif
                                     class="form-control" name="participant_question[]"&gt;
                              &lt;input type="hidden" name="participant_question_required[]"
                                     value="{{ $customQuestion-&gt;pivot-&gt;required }}"&gt;
                              &lt;input type="hidden" value="{{ $customQuestion-&gt;id }}" name="participant_question_id[]"/&gt;
                          &lt;/div&gt;
                      @endforeach
                  @endforeach
              @endforeach
              @else
                  &lt;p&gt;Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()-&gt;email : old('email')}}.&lt;/p&gt;
  
              @endif
          @endif
  
      &lt;input type="submit" href="#step2"
             id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>// registration.blade.php complete method to register the user in the congress</p>
  
  <pre><code>public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator){
      $allParticipants = Congress::where('id', $id)-&gt;first()-&gt;all_participants;
      $user = Auth::user();
  
      if($allParticipants){
          $rules = [
              'participant_name.*' =&gt; 'required|max:255|string',
              'participant_surname.*' =&gt; 'required|max:255|string',
          ];
  
          $messages = [
              'participant_question.*.required' =&gt; 'The participant is required'
          ];
  
          foreach ($request-&gt;participant_question_required as $key =&gt; $value) {
              $rule = 'string|max:255'; // I think string should come before max
              //dd($value);
              // if this was required, ie 1, prepend "required|" to the rule
              if ($value) {
                  $rule = 'required|' . $rule;
              }
  
              // add the individual rule for this array key to the $rules array
              $rules["participant_question.{$key}"] = $rule;
          }
  
          $validator = Validator::make($request-&gt;all(), $rules, $messages);
  
          if($validator-&gt;passes()) {
              $registration = Registration::create([
                  'congress_id' =&gt; $id,
                  'main_participant_id' =&gt; $user-&gt;id,
                  'status' =&gt; 'C',
              ]);
  
              $participants = [];
  
              for ($i = 0; $i &lt; count($request-&gt;participant_name); $i++)
                  $participants[] = Participant::create([
                      'name' =&gt; $request-&gt;participant_name[$i],
                      'surname' =&gt; $request-&gt;participant_surname[$i],
                      'registration_id' =&gt; $registration-&gt;id,
                      'ticket_type_id' =&gt; $request-&gt;rtypes[$i]
  
                  ]);
  
              for ($i = 0; $i &lt; count($request-&gt;participant_question); $i++)
                  $answer = Answer::create([
                      'question_id' =&gt; $request-&gt;participant_question_id[$i],
                      'participant_id' =&gt; $participants[$i]-&gt;id,
                      'answer' =&gt; $request-&gt;participant_question[$i],
                  ]);
              }
  
          return response()-&gt;json([
              'success' =&gt; true,
              'message' =&gt; 'success'
          ], 200);
      }
  
  
      else {
  
          $messages = [
              'participant_question.*.required' =&gt; 'The participant is required'
          ];
  
  
          foreach ($request-&gt;participant_question_required as $key =&gt; $value) {
              $rule = 'string|max:255'; // I think string should come before max
              //dd($value);
              // if this was required, ie 1, prepend "required|" to the rule
              if ($value) {
                  $rule = 'required|' . $rule;
              }
  
              // add the individual rule for this array key to the $rules array
              $rules["participant_question.{$key}"] = $rule;
          }
  
  
          $validator = Validator::make($request-&gt;all(), $rules, $messages);
  
  
          if ($validator-&gt;passes()) {
  
              $registration = Registration::create([
                  'congress_id' =&gt; $id,
                  'main_participant_id' =&gt; $user-&gt;id,
                  'status' =&gt; 'C',
  
              ]);
  
              $participants = [];
  
              for ($i = 0; $i &lt; count($request-&gt;participant_name); $i++)
                  $participants[] = Participant::create([
                      'name' =&gt; '',
                      'surname' =&gt; '',
                      'registration_id' =&gt; $registration-&gt;id,
                      'ticket_type_id' =&gt; $request-&gt;rtypes[$i]
  
                  ]);
  
              for ($i = 0; $i &lt; count($request-&gt;participant_question); $i++)
                  $answer = Answer::create([
                      'question_id' =&gt; $request-&gt;participant_question_id[$i],
                      'participant_id' =&gt; $participants[$i]-&gt;id,
                      'answer' =&gt; $request-&gt;participant_question[$i],
                  ]);
          }
  
          return response()-&gt;json([
              'success' =&gt; true,
              'message' =&gt; 'success'
          ], 200);
  
      }
  }
  </code></pre>
  
  <p>Relevant models to the question:</p>
  
  <pre><code>class Congress extends Model
  {
      // A conference has many ticket types
      public function ticketTypes(){
          return $this-&gt;hasMany('App\TicketType', 'congress_id');
      }
  
      public function registrations(){
          return $this-&gt;hasMany('App\Registration', 'congress_id');
      }
  }
  
  // RegistrationModel
  class Registration extends Model
  {
      // a registration has one user that do the registration (main_participant_id)
      public function customer(){
          return $this-&gt;belongsTo('App\User');
      }
      public function congress(){
          return $this-&gt;belongsTo('App\Congress');
      }
  }
  
  class TicketType extends Model
  {
      public function congress(){
          return $this-&gt;belongsTo('App\Congress');
      }
  }
  
  class Question extends Model
  {
      public function registration_type(){
          return $this-&gt;belongsToMany('App\RegistrationType', 'ticket_type_questions')
              -&gt;withPivot('required');
      }
  }
  
  class Answer extends Model
  {
      public function question(){
          return $this-&gt;belongsTo('Question');
      }
      public function participant(){
          return $this-&gt;belongsTo('Participant');
      }
  }
  </code></pre>
  
  <p>Diagram that shows some issues with the code that I have for now that is the code  in the question:</p>
  
  <p><a href="https://i.stack.imgur.com/4FrXj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4FrXj.png" alt="enter image description here"></a></p>