Saturday, 4 August 2018

Why the info of the registration types associated with the registration are appearing twice?

I have a PaymentController where I return to the view the registrationTypeDetails, the total and type_counts. So is possible to show a summary of the registration like below:

+-------------------+----------+---------+--------------+
| Registration Type | Quantity |  Price  |   Subtotal   |      
+-------------------+----------+---------+--------------+
| geral             | 2        | 10.00 € |      20.00 € |
+-------------------+----------+---------+--------------+
| TOTAL             |          |         |      20.00 € |
+-------------------+----------+---------+--------------+

But it's appearing like below, with repeated info:

+-------------------+----------+---------+--------------+
| Registration Type | Quantity |  Price  |   Subtotal   |      
+-------------------+----------+---------+--------------+
| geral             | 2        | 10.00 € |      20.00 € |
| geral             | 2        | 10.00 € |      20.00 € |
+-------------------+----------+---------+--------------+
| TOTAL             |          |         |      20.00 € |
+-------------------+----------+---------+--------------+

Do you know why the info of the registration types associated with the registration are appearing twice?

I have a PaymentController where I get the registration types info associated with the registration:

$registrationTypeDetails = Registration::with([
    'participants.registration_type',
    'participants' => function ($query) use ($registrationID) {
        $query->select('id', 'registration_type_id', 'registration_id')
              ->where('registration_id', $registrationID);
    }
])->find($registrationID);

And also some code to count each registration type asssociated with the registration:

$type_counts = [];

foreach ( $registrationTypeDetails->participants as $p ) {
    $name = $p->registration_type->name;

    if ( ! isset($type_counts[$name]) ) {
        $type_counts[$name] = 0;
    }

    $type_counts[$name]++;
}

In the view:

<div>
    <ul>
        <li>
            <span>Registration Type</span>
            <span>Quantity</span>
            <span>Price</span>
            <span>Subtotal</span>
        </li>

        @foreach( $registrationTypeDetails->participants as $participant )
            <li>
                <span></span>
                <span></span>
                <span>$</span>
                <span>$</span>
            </li>
        @endforeach

        <li>
            <span>TOTAL</span>
            <span>$</span>
        </li>
    </ul>
</div>

Maybe the issue is in the part where the info is show with "@foreach( $registrationTypeDetails->participants as $participant )...".

Because " @foreach($registrationTypeDetails->participants as $participant) " shows two participants so maybe because of that shows twice the same info.

But do you know how to correct the error?


Complete method payment() of the PaymentController, that has the code above to show the necessary info in the payment page:

class PaymentController extends Controller
{

// method that shows the payment page
public function payment($id, $slug, $regID)
{
    $regPaid = Registration::where('id', $regID)->pluck('status')->first();

    // if the registration is incomplete because the user didnt pay yet 
    if ($regPaid == "I") {

        // get the current user
        $user_id = Auth::id();

        $conferenceDetails = Registration::with([
            'conference' => function ($query) {
                $query->select('id', 'name', 'start_date');
            }
        ])->find($regID);

        $registrationTypeDetails = Registration::with(['participants.registration_type',
            'participants' => function ($query) use ($regID) {
                $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
            }
        ])->find($regID);

        $price = $registrationTypeDetails->participants->sum(function ($participant) {
            return $participant->registration_type->price;
        });

        $total = $price;
        $totalStripe = $total * 100;

        if ($registrationTypeDetails->main_participant_id != $user_id) {
            return redirect('/');
        } else {
            $type_counts = [];
            foreach ($registrationTypeDetails->participants as $p) {
                $name = $p->registration_type->name;
                if (!isset($type_counts[$name])) {
                    $type_counts[$name] = 0;
                }
                $type_counts[$name]++;
            }

            Session::put('total', $total);
            Session::put('totalStripe', $totalStripe);
            Session::put('registrationID', $regID);

            $conferenceDetails = [
                'name' => $conferenceDetails->conference->name,
                'start_date' => $conferenceDetails->conference->start_date
            ];

            Session::put('conference_name', $conferenceDetails['name']);
            Session::put('date', $conferenceDetails['end_date']);

            return view('conferences.payment', compact('conferenceDetails', 'name', 'total', 'type_counts', 'registrationTypeDetails', 'id', 'slug'));
        }
    } else {

        Session::flash('registration_complete', 'Your registration is already paid.');
        return redirect(route('user.index', ['user' => Auth::id()]) . '#myTickets');
    }

}



from Why the info of the registration types associated with the registration are appearing twice?

No comments:

Post a Comment