如何在答案表中正确存储每个答案? (得到SQLSTATE [23000]错误)

时间:2018-04-24 19:23:40

标签: laravel

我在下面有这个鳕鱼:

  • 使用registration_id,ticket_type_和电子邮件以及每个参与者的姓名在参与者表中创建一个条目。这是第一个并且工作正常。
  • 在答案表中创建一个条目,其中包含上述步骤中的参与者ID和question_id。第二步不起作用。什么时候"接下来"点击它出现:

SQLSTATE [23000]:完整性约束违规:1048列' participant_id'不能为空(SQL:插入answersquestion_idparticipant_idanswerupdated_atcreated_at)值(00 ,, 00) ,2018-04-24 19:16:10,2018-04-24 19:16:10))

//first for and is working fine
for($i = 0; $i < count($request->participant_name); $i++)
    $participant = Participant::create([
        'name' => $request->participant_name[$i],
        'surname' => $request->participant_surname[$i],
        'registration_id' => $registration->id,
        'ticket_type_id' => $request->ttypes[$i]
    ]);
// second for, this is not working
for($i = 0; $i < count($request->participant_question); $i++)
    $answer = Answer::create([
        'question_id' => $request->participant_question[$i],
        'participant_id' => $participant[$i],
        'answer' => $request->participant_question[$i],
    ]);

你知道问题出在哪里吗?

有关背景的更详细说明:

我有一个single.blade.php,用于显示会议详细信息页面。在这个会议详细信息页面中,还有一个表格供用户选择会议的每张票的门票和数量。用户点击&#34;下一步&#34;代码转到RegistrationController storeQuantities()方法:

public function storeQuantities(Request $request, $id, $slug = null){
    $ttypeQuantities = $request->get('ttypes');

    $all_participants = Congress::where('id', $id)->first()->all_participants;

    foreach($ttypeQuantities as $ttypeName => $quantity){
        if($quantity) {
            $ttype = TicketType::where('name', $ttypeName)->firstOrFail();
            $price = $ttype->price;

            $selectedType[$ttype->name]['quantity'] = $quantity;
            $selectedType[$ttype->name]['price'] = $price;
            $selectedType[$ttype->name]['subtotal'] = $price * $quantity;
            $selectedType[$ttype->name]['questions'] = $ttype->questions;
        }
    }
    Session::put('selectedTypes', $selectedTypes);
    Session::put('all_participants' , $all_participants);
    Session::put('customQuestions' ,  $selectedTypes[$ttype->name]['questions']);
    return redirect(route('congresses.registration',['id' => $id, 'slug' => $slug]));
}

然后storeUserInfo()方法将用户重定向到用户需要的registration.blade.php:

  • 输入他的姓名和电子邮件
  • 然后为每个选定的票证输入与该特定票证相关联的参与者的姓名和姓氏
  • 然后每个故障单类型都可以有自定义问题,例如&#34;您的电话号码是什么?&#34;,因此如果用户在上一页中选择了具有自定义问题的故障单类型,则自定义问题也会显示给用户,所以他可以回答

Registration.blade.php代码,用于显示收集上述数据的字段:

<form method="post" action="">
    {{csrf_field()}}
    <div class="form-group font-size-sm">
        <label for="name" class="text-gray">Name</label>
        <input type="text" required class="form-control" id="name"
               name="name" value="{{ (\Auth::check()) ? Auth::user()->name : old('name')}}">
    </div>
    <div class="form-group font-size-sm">
        <label for="surname" class="text-gray">Surname</label>
        <input type="text" id="surname" required class="form-control" name="surname" value="{{ (\Auth::check()) ? Auth::user()->surname : old('surname')}}">
    </div>

<!-- other form fields -->

<!-- if the all_participants is 1 in the confernece table it should appear for each selected ticket a section for the user 
    that is doing the registration insert the name and surname of each paarticipant -->
    @if (!empty($all_participants))
        @if($all_participants == 1)
            @foreach($selectedTypes as $k=>$selectedType)
              @foreach(range(1, $selectedType['quantity']) as $test)
                  <h6>Participant - 1 - {{$k}}</h6> <!-- $k shows the ticket type name -->
                  <div class="form-group font-size-sm">
                      <label for="participant_name" class="text-gray">Name</label>
                      <input type="text" name="participant_name[]" required class="form-control" value="">
                  </div>
                  <div class="form-group font-size-sm">
                      <label for="participant_surname" class="text-gray">Surname</label>
                      <input type="text" required class="form-control" name="participant_surname[]" value="">
                  </div>
                   @foreach($selectedType['questions'] as $customQuestion)
                        <div class="form-group">
                            <label for="participant_question">{{$customQuestion->question}}</label>
                            <input type="text" required class="form-control" name="participant_question[]" value=""> 
                            <input type="hidden" value="{{ $customQuestion->id }} name="participant_question_id[]"/>


                        </div>
                    @endforeach
              @endforeach
        @endif
    @endif
    <input type="submit" href="#" value="Next"/>
  </form>

然后当用户点击&#34;下一步&#34;代码转到storeUserInfo()

public function storeUserInfo(Request $request, $id, $slug = null, Validator $validator){
    $user = Auth::user();
    $registration = Registration::create([
        'congress_id' => $id,
        'main_participant_id' => $user->id,
        'status' => 'C',
    ]);
    // the code in this for is working
    for($i = 0; $i < count($request->participant_name); $i++)
        $participant = Participant::create([
            'name' => $request->participant_name[$i],
            'surname' => $request->participant_surname[$i],
            'registration_id' => $registration->id,
            'ticket_type_id' => $request->ttypes[$i]

        ]);
    // the code in this for is not working
    for($i = 0; $i < count($request->participant_question); $i++)
        $answer = Answer::create([
            'question_id' => $request->participant_question[$i],
            'participant_id' => $participant[$i],
            'answer' => $request->participant_question[$i],
        ]);
}

1 个答案:

答案 0 :(得分:1)

因为$participant不是数组,所以它是一个Eloquent对象。

在创建所有参与者之前(在for循环之前),创建一个空数组:

$participants = [];

更改以下行

$participant = Participant::create([

$participants[] = Participant::create([

最后,在创建答案时更改行:

'participant_id' => $participants[$i],

'participant_id' => $participants[$i]->id,

这应该可以修复您的代码。