你知道为什么会出现“为foreach()提供的无效参数”吗?

时间:2018-06-15 11:33:44

标签: php laravel

我有这个storeRegistration()来创建一个新的注册。但它出现了一个错误:

Invalid argument supplied for foreach()

在storeRegistration()的这个foreach中:

if (isset($request->participant_question)) {
    foreach( $request->participant_question as $key => $question ) { // error is here
        $answer = Answer::create([
            'question_id' => $request->participant_question_id[$key],
            'participant_id' => $participants[$key]->id,
            'answer' => $request->participant_question[$key],
        ]);
    }
}

上面的这个foreach是在答案表中插入用户对每个问题的回答。

你知道为什么会出现这个错误吗?代码似乎是正确的。

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([
            'conference_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,
                    'answer' => $request->participant_question[$key],
                ]);
            }
        }
        return redirect(route('user.index', ['user' => Auth::id()]).'#myTickets');
    }
    else{
        //dd($validator);
        dd($validator->errors());
    }
}

用户填写的Html表单:

  <form method="post" id="registration_form" action="http://proj.test/conference/1/conf-title/registration/storeRegistration">
      <input type="hidden" name="_token" value="">

      <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>

      <div class="form-group">
        <label for="participant_question">Receive notifications?</label>
        <div class='form-check'>
          <input type='radio' name='participant_question[]' value='yes' class='form-check-input' required>    <label class="form-check-label" for="exampleCheck1">yes</label>
        </div> 
        <div class='form-check'>
          <input type='radio' name='participant_question[]' value='no' class='form-check-input' required>    <label class="form-check-label" for="exampleCheck1">no</label>
        </div>

        <input type="hidden" name="participant_question_required[]" value="1">
        <input type="hidden" value="2" name="participant_question_id[]"/>
      </div>
      <div class="form-group">
        <label for="participant_question">File</label>

        <input type='file' name='participant_question' class='form-control' required>
        <input type="hidden" name="participant_question_required[]"  value="1">
        <input type="hidden" value="3" name="participant_question_id[]"/>
      </div>

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

      <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>

      <div class="form-group">
        <label for="participant_question">Receive notifications?</label>
        <div class='form-check'>
          <input type='radio' name='participant_question[]' value='yes' class='form-check-input' required>    <label class="form-check-label" for="exampleCheck1">yes</label>
        </div> 
        <div class='form-check'>
          <input type='radio' name='participant_question[]' value='no' class='form-check-input' required>    <label class="form-check-label" for="exampleCheck1">no</label>
        </div>

        <input type="hidden" name="participant_question_required[]" value="1">
        <input type="hidden" value="2" name="participant_question_id[]"/>
      </div>
      <div class="form-group">
        <label for="participant_question">File</label>
        <input type='file' name='participant_question' class='form-control' required>
        <input type="hidden" name="participant_question_required[]" value="1">
        <input type="hidden" value="3" name="participant_question_id[]"/>
      </div>

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

3 个答案:

答案 0 :(得分:2)

根据documentation

  

foreach构造提供了一种迭代数组的简单方法。 Foreach仅适用于数组和对象,当您尝试在具有不同数据类型或未初始化变量的变量上使用时,会发出错误。

所以在foreach之前:

$participant_question = $request->participant_question;

然后你可以使用:

foreach( $participant_question as $key => $question ) {
    ...
}

我会重写它,但是:

  

Foreach仅适用于数组和对象

所以请确保$request->participant_questionarrayobject

答案 1 :(得分:1)

确实有人猜测$request->participant_question的内容会是什么。

您对各种表单字段使用相同的名称,有时作为数组但有时不使用:

  • 电话(未设置为阵列)
  • 通知
  • 文件

您需要使用唯一的名称属性来确保它们包含您认为包含的内容。

答案 2 :(得分:1)

foreach 之前检查 $ request-&gt; participant_question 是否为数组。

if (is_array($request->participant_question)) {
    foreach( $request->participant_question as $key => $question ) { // error is here
        $answer = Answer::create([
            'question_id' => $request->participant_question_id[$key],
            'participant_id' => $participants[$key]->id,
            'answer' => $request->participant_question[$key],
        ]);
    }
}