我在 session 注册页面中有一个表格,供用户在 session 中注册该表格具有以下操作:
<form method="post" action="{{route('conferences.storeRegistration', ['id' => $id, 'slug' => $slug])}}">
...
</form>
一个 session 可以有一种或多种注册类型,注册类型可以是免费的也可以是付费的。根据用户选择的注册类型,提交表单时,在此 storeRegistration()
中,我想在注册表的状态列中存储值“C”(完整) 如果所选注册类型的总价为“0”,但如果为“>0”,我想存储为“I”(不完整)。
对于 storeRegistrationInfo() 中的那个,我试图在 session “总计”中获取值,然后使用三元“状态”=> ($total > 0)? 'I' : 'C',
' 如下所示:
public function storeRegistration(Request $request, $id, $slug = null, Validator $validator)
{
...
$total = Session::get('total');
if ($validator->passes()) {
$registration = Registration::create([
'conference_id' => $id,
'main_participant_id' => $user->id,
'status' => ($total > 0) ? 'I' : 'C',
]);
...
}
但它不起作用,总是存储在“状态”列中的值 C',独立于注册是否已支付门票或所有门票都是免费的。问题应该是因为 dd($total) 显示“null”。
您知道如何正确实现这一点吗?
问题的顺序流:
我有一个 session 详细信息页面,用户可以在其中以表格形式选择他想要的 session 门票数量。一场 session 可以关联一张或多张票,有些票可以免费,也可以付费。所以在这个表单中,用户可以选择他想要的票,然后单击“下一步”。表单具有此操作:
<form method="post" action="{{route('conferences.storeQuantities', ['id' => $conference->id, 'slug' => $conference->slug])}}">
...
</form>
因此,当用户单击“下一步”时,代码转到 RegistrationController storeQuantities() 此方法存储所选票证(每张票的数量、总数等)和 session 中的一些其他信息,并将用户返回到'conferences.registration' 路线:
public function storeQuantities(Request $request, $id, $slug = null)
{
$request->validate([
'rtypes' => ['required', 'array', new RegistrationTypeQuantity],
]);
$rtypeQuantities = $request->get('rtypes');
$allParticipants = Conference::where('id', $id)->first()->all_participants;
$total = 0;
foreach ($rtypeQuantities as $rtypeName => $quantity) {
if ($quantity) {
$rtype = RegistrationType::where('name', $rtypeName)->firstOrFail();
$price = $rtype->price;
$selectedRtypes[$rtype->name]['quantity'] = $quantity;
$selectedRtypes[$rtype->name]['price'] = $price;
$selectedRtypes[$rtype->name]['subtotal'] = $price * $quantity;
$total += $selectedRtypes[$rtype->name]['subtotal'];
$selectedRtypes[$rtype->name]['total'] = $total;
$selectedRtypes[$rtype->name]['questions'] = $rtype->questions;
$selectedRtypes[$rtype->name]['id'] = $rtype->id;
}
}
Session::put('selectedRtypes', $selectedRtypes);
Session::put('allParticipants', $allParticipants);
Session::put('customQuestions', $selectedRtypes[$rtype->name]['questions']);
Session::put('total', $total);
return redirect(route('conferences.registration', ['id' => $id, 'slug' => $slug]))->with('total', $total);
}
然后代码转到路由:
Route::get('/conference/{id}/{slug?}/registration', [
'uses' => 'RegistrationController@displayRegistrationPage',
'as' =>'conferences.registration'
]);
然后函数 displayRegistrationPage 被调用,它获取 session 中的值并将用户重定向到注册页面:
public function displayRegistrationPage(Request $request, $id, $slug = null)
{
$selectedRtypes = Session::get('selectedRtypes');
$allParticipants = Session::get('allParticipants');
$customQuestions = Session::get('customQuestions');
if (isset($selectedRtypes)) {
return view('conferences.registration',
['selectedRtypes' => $selectedRtypes, 'allParticipants' => $allParticipants, 'customQuestions' => $customQuestions, 'id' => $id,
'slug' => $slug]);
} else {
return redirect(route('conferences.show', ['id' => $id, 'slug' => $slug]));
}
}
现在注册页面呈现给用户。这里有一个用户注册表格,介绍一些数据以在 session 中注册。表单具有此操作:
<form method="post" action="{{route('conferences.storeRegistration', ['id' => $id, 'slug' => $slug])}}">
...
</form>
因此,当提交表单时,如果用户正在进行的注册只有免费注册类型,那么在这个 storeRegistration() 中,我想在注册表的状态列中存储值“C”(完成)。但我想存储值“I”(不完整),如果有 1 种或多种注册类型,则价格 > 0。因为用户随后需要付款,所以注册在他付款之前保持不完整。
public function storeRegistration(Request $request, $id, $slug = null, Validator $validator)
{
...
$total = Session::get('total');
if ($validator->passes()) {
$registration = Registration::create([
'conference_id' => $id,
'main_participant_id' => $user->id,
'status' => ($total > 0) ? 'I' : 'C',
]);
...
...
}
完整注册 Controller :
class RegistrationController extends Controller
{
// handles the form of the conference details page where the user select the tickets
public function storeQuantities(Request $request, $id, $slug = null)
{
$request->validate([
'rtypes' => ['required', 'array', new RegistrationTypeQuantity],
]);
$rtypeQuantities = $request->get('rtypes');
$allParticipants = Conference::where('id', $id)->first()->all_participants;
$total = 0;
foreach ($rtypeQuantities as $rtypeName => $quantity) {
if ($quantity) {
$rtype = RegistrationType::where('name', $rtypeName)->firstOrFail();
$price = $rtype->price;
$selectedRtypes[$rtype->name]['quantity'] = $quantity;
$selectedRtypes[$rtype->name]['price'] = $price;
$selectedRtypes[$rtype->name]['subtotal'] = $price * $quantity;
$total += $selectedRtypes[$rtype->name]['subtotal'];
$selectedRtypes[$rtype->name]['total'] = $total;
$selectedRtypes[$rtype->name]['questions'] = $rtype->questions;
$selectedRtypes[$rtype->name]['id'] = $rtype->id;
}
}
Session::put('selectedRtypes', $selectedRtypes);
Session::put('allParticipants', $allParticipants);
Session::put('customQuestions', $selectedRtypes[$rtype->name]['questions']);
Session::put('total', $total);
return redirect(route('conferences.registration', ['id' => $id, 'slug' => $slug]))->with('total', $total);
}
// redirects the user to the registration page
public function displayRegistrationPage(Request $request, $id, $slug = null)
{
$selectedRtypes = Session::get('selectedRtypes');
$allParticipants = Session::get('allParticipants');
$customQuestions = Session::get('customQuestions');
if (isset($selectedRtypes)) {
return view('conferences.registration',
['selectedRtypes' => $selectedRtypes, 'allParticipants' => $allParticipants, 'customQuestions' => $customQuestions, 'id' => $id,
'slug' => $slug]);
} else {
return redirect(route('conferences.show', ['id' => $id, 'slug' => $slug]));
}
}
// handles the registration form of the registration page
public function storeRegistration(Request $request, $id, $slug = null, Validator $validator)
{
$allParticipants = Conference::where('id', $id)->first()->all_participants;
$user = Auth::user();
$total = Session::get('total');
$rules = [];
$messages = [];
if (isset($request->participant_question_required)) {
$messages = [
'participant_question.*.required' => 'Fill all mandatory fields.',
];
foreach ($request->participant_question_required as $key => $value) {
$rule = 'string|max:255';
// 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 == 1) {
$rules["participant_name.*"] = 'required|max:255|string';
$rules["participant_surname.*"] = 'required|max:255|string';
}
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->passes()) {
$registration = Registration::create([
'conference_id' => $id,
'main_participant_id' => $user->id,
'status' => ($total > 0) ? 'I' : '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)) {
foreach( $request->participant_question as $key => $question ) {
$answer = Answer::create([
'question_id' => $request->participant_question_id[$key],
'participant_id' => $participants[$key]->id,
'answer' => $request->participant_question[$key],
]);
}
}
return redirect(route('user.index', ['user' => Auth::id()]).'#myTickets');
}
}
}
上下文布局:https://ibb.co/b0VN8T
最佳答案
你的问题在这里:
return redirect(route('conferences.registration', ['id' => $id, 'slug' => $slug]))->with('total', $total);
您已经以永久方式将数据存储到 session 中,但就在您想要重定向时,您删除了第一个存储的数据:
Session::put('total', $total);
与:
...->with('total', $total);
最后一个,仅将数据闪入您的 session 以供下一个请求(当您显示注册表时),因此当您发送注册表以供提交时,“总计”数据已经从 session 中消失。
删除:
...->with('total', $total);
这对你来说没问题。
关于php - 存储注册状态不正常(始终存储值 "C",但如果注册已支付,则应存储 "I"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548256/