如何组织此上下文以正确收集每个参与者的答案?

时间:2018-06-13 11:06:07

标签: php laravel

我在会议中有一个用户注册表。但是,当用户点击"商店注册"它显示了一个未定义的偏移错误,问题应该是因为我没有正确组织这个上下文。在会议中注册的表单会有所不同,具体取决于" all_participants"会议桌的列是" 1"或" 0"。

如果会议桌中的all_participants为1

如果会议桌有列" all_participants"有价值" 1"这意味着有必要收集每个参与者的姓名和姓氏(关于每个选定的注册类型)。因此,表单将显示表单字段以收集每个参与者的姓名和姓氏。此外,如果某些选定的注册类型具有关联的自定义问题,则需要为每个参与者收集问题的答案,如果" all_participants"是1.在上面的图像中注册类型"一般"有一个自定义问题关联"电话",因此有必要收集两个参与者的答案,并注册了注册类型"一般"。

将all_participants的形式显示为" 1"在会议桌

enter image description here

用户点击"商店注册"它应该存储在如下表格中:

enter image description here

如果会议桌中的all_participants为0

如果" all_participants"是" 0"表单只有一个字段" Phone"因为名称和姓氏直接来自经过身份验证的用户信息。因为" all_participants"是" 0"仅需要收集正在进行注册的用户的电话(经过身份验证的用户),因为其他参与者不是必需的,因为" all_participants"是" 0"。所以表格如果" all_participants"是" 0"在这种情况下,只有一个字段(电话)。参与者表只需要存储注册用户的姓名和姓氏,其他参与者可以空存储""。

如果all_participants是" 0":

,则表格

enter image description here

用户点击"商店注册"它应该存储在如下表格中:

enter image description here

此图像上下文的HTML,用于" all_participants"是1:

<form method="post" id="registration_form" action="http://proj.test/conference/1/conference-title/registration/storeRegistration">

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

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

  <div class="form-group">
    <label for="participant_question">Phone?</label>
    <input type="text" name="participant_question[]" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="1" name="participant_question_id[]">
  </div>

  <input type="hidden" name="rtypes[]" value="1">

  <h6> Participant - 2 - general</h6>

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

  <div class="form-group">
    <label for="surnamegeneral_2">Surname</label>
    <input type="text" id="surnamegeneral_2" required="" class="form-control" name="participant_surname[]" value="">
  </div>

  <div class="form-group">
    <label for="participant_question">Phone?</label>
    <input type="text" name="participant_question[]" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="1" name="participant_question_id[]">
  </div>

  <input type="hidden" name="rtypes[]" value="1">

  <h6> Participant - 1 - plus</h6>

  <div class="form-group font-size-sm">
    <label for="nameplus_1">Name</label>
    <input type="text" id="nameplus_1" name="participant_name[]" required="" class="form-control" value="">
  </div>

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

  <input type="hidden" name="rtypes[]" value="2">

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

当用户点击&#34;商店注册&#34;代码转到StoreRegistration()以将所有注册信息存储在数据库中:

public function storeRegistration(Request $request, $id, $slug = null)
  {
      $allParticipants = Conference::where('id', $id)->first()->all_participants;
      $user = Auth::user();

      $rules = [];
      $messages = [];

      if ($allParticipants == 1) {

          $rules["participant_name.*"] = 'required|max:255|string';
          $rules["participant_surname.*"] = 'required|max:255|string';
      }

      $validator = Validator::make($request->all(), $rules);

      if ($validator->passes()) {

          $total = Session::get('total');

          $registration = Registration::create([
              'conferenec_id' => $id,
              'main_participant_id' => $user->id,
              'status' => ($total > 0) ? 'I' : '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,
                  'registration_type_id' => $request->rtypes[$i]

              ]);
          }

          if (isset($request->participant_question)) {
              foreach( $request->participant_question as $key => $question ) {
                  $answer = Answer::create([
                      'question_id' => $request->participant_question_id[$key],
                      'participant_id' => $participants[$key]->id, // undefined index error is here
                      'answer' => $request->participant_question[$key],
                  ]);
              }
          }
          return redirect(route('user.index', ['user' => Auth::id()]).'#myTickets');
      }
      else{
          dd($validator->errors());
      }
  }

我有一个带有getHtmlInput()的Question模型来为自定义问题生成HTML,如果&#34; required&#34;那么将所需的属性添加到字段中。数据透视表中的列&#34; registration_type_questions&#34;是&#34; 1&#34;:

class Question extends Model
{
    protected $fillable = [
        'question', 'type', 'conference_id',
    ];
    public static $typeHasOptions = [
        'radio_btn',
        'select_menu',
        'checkbox'
    ];
    public function registration_type()
    {
        return $this->belongsToMany('App\RegistrationType', 'registration_type_questions')
            ->withPivot('required');
    }
    public function options()
    {
        return $this->hasMany('App\QuestionOption');
    }
    public function hasOptions()
    {
        return in_array($this->type, self::$typeHasOptions);
    }
    public function getHtmlInput($name = "", $options = "", $required = false, $customtype = false)
    {
        $html = '';
        $html .= $customtype == 'checkbox' ? "<div class='checkbox-group ".($required ? " required" : "")."'>" : '';
        $html .= $customtype == 'select_menu' ? "<select name='participant_question[]' class='form-control' " . ($required ? " required" : "")
            . ">" : '';

        if (empty($options)) {
            switch ($customtype) {
                case "text":

                    $html .= " 
                <input type='text' name='participant_question[]' class='form-control'" . ($required ? " required" : "")
                        . ">";
                    break;

                case "file":
                    $html .= " 
                <input type='file' name='participant_question[]' class='form-control'" . ($required ? " required" : "") . ">";
                    break;

                case "long_text":
                    $html .= "
            <textarea name='participant_question' class='form-control' rows='3'" . ($required ? " required" : "") . ">"
                        . $name .
                        "</textarea>";
                    break;
            }
        } else {
            foreach ($options as $option) {
                switch ($customtype) {
                    case "checkbox":
                        $html .= " 
        <div class='form-check'>
            <input type='checkbox' name='participant_question[]' value='" . $option->value . "' class='form-check-input' >
                <label class='form-check-label' for='exampleCheck1'>" . $option->value . "</label>
        </div>";
                        break;
                    case "radio_btn":
                        $html .= " 
            <div class='form-check'>
                <input type='radio' name='participant_question[]' value='" . $option->value . "' class='form-check-input'" . ($required ? " required" : "") . ">" .
                            '    <label class="form-check-label" for="exampleCheck1">' . $option->value . '</label>' .
                            "</div>";
                        break;
                    case "select_menu":
                        $html .= "<option value='" . $option->value . "'>" . $option->value . "</option>";
                        break;
                }
            }
        }
        $html .= $customtype == 'select_menu' ? "</select>" : '';
        $html .= $customtype == 'checkbox' ? "</div>" : '';

        return $html;
    }
}

然后在视图中使用getHtmlInput(),如:

@foreach($selectedRtype['questions'] as $customQuestion)
  <div class="form-group">
      <label for="participant_question">{{$customQuestion->question}}</label>
      @if($customQuestion->hasOptions() && in_array($customQuestion->type, ['checkbox', 'radio_btn', 'select_menu']))
          {!! $customQuestion->getHtmlInput(
              $customQuestion->name,
              $customQuestion->options,
              ($customQuestion->pivot->required == '1'),
              $customQuestion->type)
          !!}

      @else
          {!! $customQuestion->getHtmlInput(
              $customQuestion->name,
              [],
              ($customQuestion->pivot->required == '1'),
              $customQuestion->type)
          !!}
      @endif
      <input type="hidden"
             name="participant_question_required[]"
             value="{{ $customQuestion->pivot->required }}">
      <input type="hidden"
             value="{{ $customQuestion->id }}"
             name="participant_question_id[]"/>
  </div>
@endforeach

1 个答案:

答案 0 :(得分:11)

我稍微修改了HTML文件结构。

<form method="post" id="registration_form" action="http://proj.test/conference/1/conference-title/registration/storeRegistration">

{{csrf_field()}}

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

<div class="form-group">
    <label for="surnamegeneral_1">Surname</label>
    <input type="text" id="surnamegeneral_1" required="" class="form-control" name="participant[1][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">
    <label for="namegeneral_2">Name</label>
    <input type="text" id="namegeneral_2" name="participant[2][name]" required="" class="form-control" value="">
</div>

<div class="form-group">
    <label for="surnamegeneral_2">Surname</label>
    <input type="text" id="surnamegeneral_2" required="" class="form-control" name="participant[2][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">

<h6> Participant - 1 - plus</h6>

<div class="form-group font-size-sm">
    <label for="nameplus_1">Name</label>
    <input type="text" id="nameplus_1" name="participant[3][name]" required="" class="form-control" value="">
</div>

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

<input type="hidden" name="participant[3][rtypes]" value="2">

<input type="submit" class="btn btn-primary" value="Store Registration">

我会将所有participants存储在名为participant的数组中,然后将其发送到Backend

输出:

array:2 [▼
  "_token" => "WDtDV0CL6OKVCsGSi5HNyi4HQ6Pmo6VAwzDsgYK1"
  "participant" => array:3 [▼
    1 => array:5 [▼
      "name" => "ali"
      "surname" => "shahabi"
      "answer" => "0937"
      "question_id" => "1"
      "rtypes" => "1"
    ]
    2 => array:5 [▼
      "name" => "danyal"
      "surname" => "shahabi"
      "answer" => "0938"
      "question_id" => "1"
      "rtypes" => "1"
    ]
    3 => array:3 [▼
      "name" => "baba"
      "surname" => "babaei"
      "rtypes" => "2"
    ]
  ]
]

storeRegistration方法。

我从代码中删除了validation并专注于程序的逻辑:

public function storeRegistration(Request $request, $id, $slug = null)
{

    # all_participants field
    $allParticipants = Conference::where('id', $id)->first()->all_participants;

    $total = Session::get('total');

    # user object
    $user = Auth::user();

    # add registration to Database
    $registration = Registration::create([
        'conference_id' => $id,
        'main_participant_id' => $user->id,
        'status' => ($total > 0) ? 'I' : 'C',
    ]);

    # List of all participants
    $participants_list=$request->get('participant');

    #add all participants to Database
    foreach ($participants_list as $participant)
    {
        $name = ($allParticipants) ? $participant['name'] : '';
        $surname = ($allParticipants) ? $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'],
            ]);}
    }

    return redirect(route('user.index', ['user' => Auth::id()]).'#myTickets');
}