如何将每个参与者正确存储在参与者表中?

  • 本文关键字:参与者 存储 php laravel
  • 更新时间 :
  • 英文 :


我有一个 registration.blade.php用户需要输入一些数据才能在大会上注册。

例如,如果会议有 4 种票证类型"ticketype1"、"ticketype2"、"ticketype3"和"ticketype4",并且用户选择他想要 1 张票证类型为"ticketype1"的票证和 2 张票证类型为"ticketype4"的票证,它将出现在 registration.blade 中.php:

  • 用户插入其姓名和电子邮件的部分(如果用户经过身份验证,则会自动填充)。
  • 然后它出现 3 个部分,
  • 因为用户选择了 3 张票,每个部分都会询问每个参与者的姓名。每个部分都与选定的工单类型相关联。

在 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>

在用户插入此字段的信息并单击"下一步"后,代码将使用 ajax post 请求转到下面的 storeUserInfo() 方法。

怀疑:

我的疑问是将必要的信息存储在数据库中。

当在注册表中单击"下一步"按钮时,我想在storeUserInfo()方法中单击

  • 在"注册"表中创建一个条目,这已经工作正常。
  • 但是我在下一部分没有成功,也就是说,循环遍历所有条目并在参与者表中创建一个条目,其中包含每个参与者的registration_id、ticket_type_id、姓名和姓氏。因为必须有票证类型,因为存储参与者需要存储通过"ticket_type_id"列注册该参与者的票证类型。例如,在与所选工单对应的每个部分中,该工单类型的名称带有"<h6>Participant - 1 - {{$k}}</h6>"($k显示工单类型名称)。因此,进行注册的用户应输入每个参与者的姓名,同时考虑到他要注册每个参与者的票证类型。我的疑问是如何在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数组来自RegistrationControllerstoreQuantities()方法,该方法存储每种工单类型的选定数量并将用户返回到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]));
}

你必须在循环中执行此操作。假设你有一个链接到RegistrationParticipant模型,你可以执行以下操作:

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;

其次,在loop内向表单添加一个hidden输入,以便$selectedTypes值为$selectedType['id']

<input type="hidden" name="ticket_types[]" value="{{ $selectedType['id'] }}"/> 

最后,在后端,处理ticket_type与处理其他两个数组输入相同:

...
'ticket_type_id' => $request->ticket_types[$i],

最新更新