Store in the column "status" of the registrations table the value "C" if is a free registration but "I" if is a paid registration

时间:2018-06-04 16:42:53

标签: php laravel

I have a conference details page where the user can select in a form the quantity of available tickets for the conference that he wants. A conference can have 1 or many tickets and some can be free other paid. This form for the user to select the tickets that he wants has this action:

<form method="post" action="{{route('conferences.storeQuantities', ['id' => $conference->id, 'slug' => $conference->slug])}}">
...
</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:

A summary of the storeQuantities():

public function storeQuantities(Request $request, $id, $slug = null)
    {
        ....
        $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;

        Session::put('selectedRtypes', $selectedRtypes);
        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 called that 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');

    return view('conferences.registration',
    ['selectedRtypes' => $selectedRtypes, 'allParticipants' => $allParticipants, 'customQuestions' => $customQuestions, 'id' => $id,
        'slug' => $slug]);
    }

So now the registration page is presented to the user. Here there is a form for a user to register in a conference. The form is like:

<form method="post" action="{{route('conferences.storeRegistration', ['id' => $id, 'slug' => $slug])}}">
...
</form>

Doubt: 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 total registration price is "0" but if is ">0" I want to store as "I" (incomplete).

Do you know how this can be properly achieved?

public function storeRegistration(Request $request, $id, $slug = null, Validator $validator)
{
...
if ($validator->passes()) {
    $registration = Registration::create([
        'conference_id' => $id,
        'main_participant_id' => $user->id,
        'status' => 'C',   // 'C' if free 'I' if paid
    ]);
    ...
}

1 个答案:

答案 0 :(得分:1)

您可以使用if条件来检查数组中状态的类型,方法是使用general if语句,如下所示:

$registration = Registration::create([
        'conference_id' => $id,
        'main_participant_id' => $user->id,
        'status' => ($regprice < 0) ? 'C' : 'I',   // 'C' if free 'I' if paid
    ]);