Friday, March 16, 2018

Info is being stored incorrectly in the pivot table - development

Info is being stored incorrectly in the pivot table

I have form that has some fields (Whats your email?, whats your phone number?) and then the user can select if that field should be included for the registration types that exist for a conferen and also if that field should be mandatory or not for each registration type, through checkboxes. Example of the form layout: https://ibb.co/kftnPc.

At this moment there are two registration types (General and Plus) and two questions “Whats your email?” and “whats your phone?”.

So if for the question “whats your email?” all checkboxes (“Include for the registration type General”, “Include for the registration type Plus”, “mandatory for registration type General”, “mandatory for registration type Plus”) are checked and also for the question “Whats your phone?” all checkboxes are checked, it should be inserted in the pivot table “registration_type_questions” like this:

registration_type_id question_id required
 1                       1        1       
 2                       1        1    
  1                       2        1       
  2                       2        1 

But is being inserted like:

registration_type_id question_id required
  1                       2        0       
  2                       2        0     

Do you know where can be the issue?

QuestionController update method:

 public function update(Request $request, $question_id){
        $question = $request->get('question');

        foreach($question as $key => $q) {

            $outputArray = array_fill_keys($q["rtypes"], ["required" => false]);
            foreach ($outputArray as $lineKey => $line) {
                $line[$lineKey]["required"] = in_array($lineKey, $q["mandatories"]);
            }
        }

        $getQuestion = Question::find($key);
        if($getQuestion);
        $getQuestion->registration_type()->sync($outputArray);

        $this->validate($request, [
            ]);
    }

Form:

 <form method="post" class="clearfix" action="">

  

  <div class="form-row">
    <div class="form-group col">
      <div class="row">
        <div class="col">
          <table class="table table-striped table-responsive-sm">
            <thead>
              <tr>
                <th scope="col">Info</th>
                <th scope="col">Include in registration type</th>
                <th scope="col">Mandatory Field</th>
              </tr>
            </thead>
            <tbody>
              @foreach($question as $q)
              <tr>
                <td></td>
                <td>
                  @foreach($registration_type as $rtype)
                  <div class="form-check">
                    <input name="rypes[]" autocomplete="off" class="form-check-input" type="checkbox" value="" id="">
                    <label class="form-check-label" for="exampleRadios1">
                      
                    </label>
                  </div>
                  @endforeach
                </td>
                <td>
                  @foreach($registration_type as $rtype)
                  <div class="form-check">
                    <input name="mandatories[]" autocomplete="off" class="form-check-input" type="checkbox" value="" id="">
                    <label class="form-check-label" for="exampleRadios1">
                      for the registration type ""
                    </label>
                  </div>
                  @endforeach
                </td>
              </tr>
              @endforeach

            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
  <input type="submit" class="btn btn-primary float-right mt-3" value="Update"/>
</form>   



from Laravel Questions and Answers https://laravelquestions.com/php/info-is-being-stored-incorrectly-in-the-pivot-table/
via Lzo Media

No comments:

Post a Comment