【问题标题】:How to properly store each answer in the answers table? (getting SQLSTATE[23000] error)如何正确地将每个答案存储在答案表中? (出现 SQLSTATE[23000] 错误)
【发布时间】:2018-10-05 04:08:06
【问题描述】:

我在下面有这条鳕鱼:

  • 在参与者表中创建一个条目,其中包含 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 用于显示大会详细信息页面。在此大会详细信息页面中,还有一个表格供用户选择大会门票和每张门票的数量。用户单击“下一步”后,代码转到 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:

  • 输入他的姓名和电子邮件
  • 然后为每个选定的票输入要与该特定票关联的参与者的姓名和姓氏
  • 那么每个工单类型都可以有自定义问题,例如“你的电话号码是多少?”,因此如果用户在上一页中选择了具有自定义问题的工单类型,那么还会向用户显示自定义问题以便他回答

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>

然后当用户点击“下一步”时,代码会转到 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],
        ]);
}

【问题讨论】:

    标签: laravel


    【解决方案1】:

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

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

    $participants = [];
    

    更改以下行

    $participant = Participant::create([
    

    $participants[] = Participant::create([
    

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

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

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

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

    【讨论】:

    • *$participant = [];
    • @JonasStaudenmeir 我认为你的意思是单数到复数?感谢您的通知!
    • 感谢它现在出现:“SQLSTATE[HY000]:一般错误:1366 不正确的整数值:第 1 行的列 'question_id' 的 'r'(SQL:插入 answers (question_id , participant_id, answer, updated_at, created_at) 值 (r, 5, r, 2018-04-24 21:16:09, 2018-04-24 21:16:09))”。你知道为什么吗? “r”只是一个要测试的值(答案)。
    • @joW 是的,请检查您正在创建答案的部分:'question_id' =&gt; $request-&gt;participant_question[$i], 最后一部分需要是 participant_question_id[$i]
    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多