我在视图中有以下代码,供用户介绍一些数据来注册会议。然后我有RegistrationController storeRegistratioInfo()。当用户填写表单并提交表单时,会发出ajax请求,并显示错误:
{message:" Undefined offset:2",exception:" ErrorException",...} 异常:" ErrorException" file:RegistrationController.php" line:194 message:" Undefined offset:2"追踪:[,...]
第194行是:
'participant_id' => $participants[$i]->id,
完整的storeRegistration()方法如下。
视图中的代码:
@foreach($selectedRtypes as $k => $selectedRtype)
@foreach(range(1,$selectedRtype['quantity']) as $val)
@if($allParticipants == 1)
<h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">
Participant - {{$val}} - {{$k}}</h6>
<div class="form-group font-size-sm">
<label for="name{{ $k }}_{{ $val }}"
class="text-gray">Name</label>
<input type="text" id="name{{ $k }}_{{ $val }}"
name="participant_name[]" required
class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surname{{ $k }}_{{ $val }}"
class="text-gray">Surname</label>
<input type="text" id="surname{{ $k }}_{{ $val }}"
required class="form-control"
name="participant_surname[]" value="">
</div>
@foreach($selectedRtype['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">{{$customQuestion->question}}</label>
@if($customQuestion->hasOptions())
{!! $customQuestion->getHtmlInput(
$customQuestion->name,
$customQuestion->options,
($customQuestion->pivot->required == '1'),
'form-control',
$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
@else
<input type="hidden" value="foo"
name="participant_name[]"/>
<input type="hidden" value="bar"
name="participant_surname[]"/>
@endif
<input type="hidden" name="rtypes[]"
value="{{ $selectedRtype['id'] }}"/>
@endforeach
@if ($allParticipants == 0)
@foreach($selectedRtype['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">{{$customQuestion->question}}</label>
@if($customQuestion->hasOptions())
{!! $customQuestion->getHtmlInput(
$customQuestion->name,
$customQuestion->options,
($customQuestion->pivot->required == '1'),
'form-control',
$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
@endif
@endforeach
storeRegistration():
public function storeRegistration(Request $request, $id, $slug = null, Validator $validator){
$allParticipants = Conference::where('id', $id)->first()->all_participants;
$user = Auth::user();
$rules = [];
$messages = [];
if (isset($request->participant_question_required)) {
$messages = [
'participant_question.*.required' => 'Fill all mandatory fields.',
'participant_name.*.required' => 'Fill the field name.',
'participant_surname.*.required' => 'Fill the field surname.',
];
foreach ($request->participant_question_required as $key => $value) {
$rule = 'string|max:255'; // I think string should come before max
// if this was required, ie 1, prepend "required|" to the rule
if ($value) {
$rule = 'required|' . $rule;
}
// add the individual rule for this array key to the $rules array
$rules["participant_question.{$key}"] = $rule;
}
}
if ($allParticipants) {
$rules["participant_name.*"] = 'required|max:255|string';
$rules["participant_surname.*"] = 'required|max:255|string';
}
$validator = Validator::make($request->all(), $rules, $messages);
$errors = $validator->errors();
$errors = json_decode($errors);
if ($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $errors
], 422);
}
if ($validator->passes()) {
$registration = Registration::create([
'conference_id' => $id,
'main_participant_id' => $user->id,
'status' => '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))
for ($i = 0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
}
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
如果存在自定义问题,例如,&#34;接收通知&#34;生成的html就像:
<div class="form-group">
<label for="participant_question">Receive Notifications?</label>
<div class="form-check">
<input type="checkbox" name="participant_question[]" value="Yes" class="form-check-input" 1="">
<label class="form-check-label" for="exampleCheck1">Yes</label></div>
<div class="form-check">
<input type="checkbox" name="participant_question[]" value="No" class="form-check-input" 1="">
<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>
&#34; dd($ request-&gt; participant_question)&#34;在storeRegistration()中显示是否选择了第一个partipant&#34;是&#34;并为第二个参与者&#34; no&#34;所示:
array:2 [
0 => "Yes"
1 => "No"
]
dd($participant)
in:
if (isset($request->participant_question)) {
for ($i = 0; $i < count($request->participant_question); $i++) {
dd($participants);
...
节目:
array:2 [
0 => Participant {#287
#fillable: array:4 [
0 => "name"
1 => "surname"
2 => "registration_id"
3 => "registration_type_id"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:7 [
"name" => "John"
"surname" => "Keane"
"registration_id" => 24
"registration_type_id" => "1"
"updated_at" => "2018-06-03 13:07:18"
"created_at" => "2018-06-03 13:07:18"
"id" => 47
]
#original: array:7 [
"name" => "John"
"surname" => "Keane"
"registration_id" => 24
"registration_type_id" => "1"
"updated_at" => "2018-06-03 13:07:18"
"created_at" => "2018-06-03 13:07:18"
"id" => 47
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
1 => Participant {#289
#fillable: array:4 [
0 => "name"
1 => "surname"
2 => "registration_id"
3 => "registration_type_id"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:7 [
"name" => "Jake"
"surname" => "L"
"registration_id" => 24
"registration_type_id" => "1"
"updated_at" => "2018-06-03 13:07:18"
"created_at" => "2018-06-03 13:07:18"
"id" => 48
]
#original: array:7 [
"name" => "Jake"
"surname" => "L"
"registration_id" => 24
"registration_type_id" => "1"
"updated_at" => "2018-06-03 13:07:18"
"created_at" => "2018-06-03 13:07:18"
"id" => 48
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
]
答案 0 :(得分:1)
从错误中可以明显看出&#34;参与者&#34;数组没有超出FOR循环语句中规定的$ i指针的值。我也注意到,你没有正确地关闭你的IF和FOR语句。您可能也想检查一下。
if(isset($request->participant_question))
for($i=0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
答案 1 :(得分:1)
您正在遍历一系列可能会或可能不会连接到参与者数组的问题。问题数组来自POST返回的$ request:
<input type="checkbox" name="participant_question[]" value="No" class="form-check-input" 1="">
<input type="hidden" name="participant_question_required[]" value="1">
<input type="hidden" value="2" name="participant_question_id[]">
等等。
但是,您正在制作一系列参与者(并创建他们的ID)独立的问题数量。 IE你可能有11个问题在这里循环:
@foreach($selectedRtype['questions'] as $customQuestion)
因此,当你在创作方面循环时:
for($i=0; $i < count($request->participant_question); $i++)
$ i可能=== 10.但是,您可能只有2个新参与者来自表单。这意味着您的$participants[]
只会包含$participants[0]
和$participants[1]
。因此,当在问题数组(计数为11)内循环时,参与者数组在超过索引1时将失败(当$i === 2
进行$participants[2]
调用时,它将失败)数组中只有2个。
要解决此问题,您必须删除
'participant_id' => $participants[$i]->id,
来自创建数组。同样,参与者的数量与问题的数量无关,因此这将失败。您需要直接将新参与者链接到问题。可能在您创建参与者时,请在循环参与者参数循环时检查他回答了哪个问题。您无法使用现有代码执行此操作 - 您尝试链接表单上未链接的两个内容。我认为您需要更改表单上的链接,或者有办法将新参与者与他明确回答的问题相结合。但这超出了这个问题的范围 - 它不起作用的原因是你有两个不同的阵列有两个不同的数量。