我在下面有这个鳕鱼:
SQLSTATE [23000]:完整性约束违规:1048列' participant_id'不能为空(SQL:插入answers
(question_id
,participant_id
,answer
,updated_at
,created_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:
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],
]);
}
答案 0 :(得分:1)
因为$participant
不是数组,所以它是一个Eloquent对象。
在创建所有参与者之前(在for
循环之前),创建一个空数组:
$participants = [];
更改以下行
$participant = Participant::create([
到
$participants[] = Participant::create([
最后,在创建答案时更改行:
'participant_id' => $participants[$i],
到
'participant_id' => $participants[$i]->id,
这应该可以修复您的代码。