我有一个registration.blade.php,用户需要输入一些数据才能在国会注册。
例如,如果会议有4种票类型" ticketype1"," ticketype2"," ticketype3"和#34; ticketype4"并且用户选择他想要1票证类型" ticketype1" 2张门票类型和#34; ticketype4"它将出现在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>
@endforeach
@endif
@endif
<input type="submit" href="#" value="Next"/>
</form>
用户插入此字段的信息后,单击&#34;下一步&#34;代码使用ajax post请求转到下面的storeUserInfo()方法。
的疑问:
我的疑问是将必要的信息存储在数据库中。
当&#34;下一个&#34;在storeUserInfo()方法中我想要的注册表单中单击按钮:
<h6>Participant - 1 - {{$k}}</h6>
&#34; ($ k显示票证类型名称)。因此,正在进行注册的用户应该输入每个参与者的姓名和姓氏,并考虑他想要注册每个参与者的票类型。我怀疑的是如何在storeUserInfo方法中获取该信息,因此可以在参与者表中创建必要的条目。你知道如何正确实现这一目标吗?
// storeUserInfo方法
public function storeUserInfo(Request $request, $id, $slug = null, Validator $validator){
//dd($request->all());
$user = Auth::user();
$validator = Validator::make($request->all(),[
'name' => 'required|max:255|string',
'surname' => 'required|max:255|string',
'email' => 'required|max:255|string',
'participant_name.*' => 'required|max:255|string',
'participant_surname.*' => 'required|max:255|string',
]);
if($validator->passes())
{
$registration = Registration::create([
'congress_id' => $id,
'main_participant_id' => $user->id,
'status' => 'C',
]);
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
$errors = $validator->errors();
$errors = json_decode($errors);
return response()->json([
'success' => false,
'errors' => $errors
], 422);
}
dd($request->all())
显示例如:
array:7 [
"_token" => "4AdKbRyAna2il5IrOl..."
"name" => "John"
"surname" => "Keane"
"email" => "@gmail.com"
"participant_name" => array:2 [
0 => "John"
1 => "Jake"
2 => "Wilson"
]
"participant_surname" => array:2 [
0 => "Keane"
1 => "W"
2 => "L"
]
]
selectedTypes
数组来自RegistrationController
storeQuantities()
方法,该方法存储每种故障单类型的选定数量,并将用户返回到registration.blade.php
页面:
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;
}
}
Session::put('selectedTypes', $selectedTypes);
Session::put('all_participants' , $all_participants);
//dd($selectedTypes);
return redirect(route('congresses.registration',['id' => $id, 'slug' => $slug]));
}
答案 0 :(得分:0)
您必须在循环中执行此操作。假设您有Participant
的模型,该模型与Registration
相关联,您可以执行以下操作:
for($i = 0; $i < count($request->participant_name); $i++)
$participant = Participant::create([
'participant_name' => $request->participant_name[$i],
'participant_surname' => $request->participant_surname[$i],
'registration_id' => $registration->id
]);
}
由于您的输入是基于0的索引,您可以遍历其中一个数组输入(participant_name
)并使用该索引从另一个数组输入(participant_surname
)中提取值
编辑:要处理丢失的ticket_type_id,需要将其包含在发送的数据中以查看和发送到服务器的数据。
首先,将属性添加到从服务器返回到视图的每个$selectedType
:
$selectedType[$ttype->name]['id'] = $ttype->id;
其次,在hidden
loop
中为$selectedTypes
添加$selectedType['id']
输入,其值为<input type="hidden" name="ticket_types[]" value="{{ $selectedType['id'] }}"/>
ticket_type
最后,在后端,处理...
'ticket_type_id' => $request->ticket_types[$i],
与处理其他两个数组输入相同:
Spring