I have a form in the conference registration page for the user to register in a conference The form has the action below:
<form method="post" action="">
...
</form>
When the form is submitted, in this storeRegistration() I want to store in the status column of the registrations table the value "C" (complete) if the total registration price is "0" but if is ">0" I want to store as "I" (incomplete).
For that in the storeRegistrationInfo() Im trying to get get value in the session 'total' and then use a ternary 'status' => ($total > 0) ? 'I' : 'C',' like below:
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',
]);
...
...
}
But it's not working, is always stored in the column 'status' the value C', independently if the registration has paid tickets or if all tickets are free. The issue should be because dd($total) shows "null".
Do you know how this can be properly achieved?
Sequence flow of the issue:
I have a conference details page where the user can select in a form the quantity of tickets that he wants for the conference. A conference can have 1 or many tickets associated, and some can be free other paid. So in this form for the user to select the tickets that he wants and then clicks "Next". The form has this action:
<form method="post" action="">
...
</form>
So when the user clicks in "Next" the code goes to the RegistrationController storeQuantities() this method stores the selected tickets (quantity of each ticket, total, etc) and some other some info in the session and returns the user to the 'conferences.registration' route:
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();
//dd($rtype);
$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;
//dd($selectedRtypes);
}
}
//dd($rtype->questions);
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);
}
So then the code goes to the route:
Route::get('/conference/{id}/{slug?}/registration', [
'uses' => 'RegistrationController@displayRegistrationPage',
'as' =>'conferences.registration'
]);
So the function displayRegistrationPage is then called and it gets the values in session and 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');
$countries = Facades\Countries::all();
if (isset($selectedRtypes)) {
return view('conferences.registration',
['selectedRtypes' => $selectedRtypes, 'allParticipants' => $allParticipants, 'customQuestions' => $customQuestions, 'id' => $id,
'slug' => $slug, 'countries' => $countries]);
} else {
return redirect(route('conferences.show', ['id' => $id, 'slug' => $slug]));
}
}
So now the registration page is presented to the user. Here there is a form for a user to register introduce some data to register in a conference. The form has this action:
<form method="post" action="">
...
</form>
So, when the form is submited, in this storeRegistration() I want to store in the status column of the registrations table the value "C" (complete) if the registration that the user is doing has only free registration types. But I want to store the value "I" (incomplete), if there are 1 or more registration types, have a price > 0. Because the user then later needs to pay so the registration stays incomplete until he pays.
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',
]);
...
...
}
from Store the status of the registration is not working properly
No comments:
Post a Comment