#StackBounty: #php #laravel #eloquent How to get the name and price of the registration type(s) associated with a registration? (Eloque…

Bounty: 50

When the user clicks in a Pay button he is redirected to a payment page like “http://proj.test/conference/2/conference-title/payment/registration/6“, where 6 is the registration id that the user wants to pay and “2” is the conference id, so the user is paying a registration concerning the conference with id “2”.

In the registration payment page should appear a summary of the registration. For example, if a user did a registration in a conference with 3 tickets/registration types, 1 of the type “general” and 2 of the type “plus”, it should appear a summary like:

Title of the conference (ex: Conference test)
Date of the conference

Registration Type   Quantity         Price     Subtotal
 general             1            0.00 €      0.00$
 plus                2            1.00 €      2.00$

So I have this route for when the user clicks in the Pay button:

Route::get('/conference/{id}/{slug?}/payment/registration/{regID}', [
    'uses' => 'PaymentController@showSummary',
    'as'   =>'conferences.showSummary'
]);

In the showSummary() and I already get the conference name and date:

public function showSummary($id = "", $slug = "", $regID){

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

Doubt: My doubt is how to change the following query to get the name and the price of each registration type associated with the registration, and also get the quantity so is possible to show a summary like the summary above.

With:

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

        dd($registrationTypeDetails);

The $registrationTypeDetails shows the participants associated with the registration:

Registration {#264 ▼
...
  #relations: array:1 [▼
    "participants" => Collection {#261 ▼
      #items: array:2 [▼
        0 => Participant {#269 ▼
          #fillable: array:4 [▶]
          ...
          #attributes: array:3 [▼
            "id" => 3
            "registration_type_id" => 2
            "registration_id" => 2
          ]
         ...
        }
        1 => Participant {#271 ▼
          #fillable: array:4 [▶]
          ....
          #attributes: array:3 [▼
            "id" => 4
            "registration_type_id" => 3
            "registration_id" => 2
          ]
         ....
        }
      ]
    }
  ]
}

So with the above query is possible to get to the registration_type_id of the participants associated to the registration and so should be possible to get the info about each registration type but I’m not understanding how to change the query to achieve that.

Do you know how to achieve that?


Relevant tables structure for the question:

Registration: id, status, conference_id, main_participant_id   
    (main_participant_id is the id of the user that did the regitration)

Registration Types: id, name, price, conference_id

Conference: id, name, date, organizer_id

Participant: id, registration_id, registration_type_id, name, surname

Relevant models for the question:

User model:

 class User extends Authenticatable
    {
        public function conferences(){
            return $this->hasMany('AppConference', 'organizer_id');
        }
        public function registrations(){
            return $this->hasMany('AppRegistration','main_participant_id');
        }
    }

Conference model:

class Conference extends Model
{
    public function organizer(){
        return $this->belongsTo('AppUser', 'organizer_id');
    }
    public function registrationTypes(){
        return $this->hasMany('AppRegistrationType', 'conference_id');
    }
    public function registrations(){
        return $this->hasMany('AppRegistration', 'conference_id');
    }
}

Registration Type model:

class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('AppConference');
    }

    // a registration can have many participants
    public function participants(){
       return $this->hasMany('AppParticipant');
    }
}

Registration model:

class Registration extends Model
{
    // a registration has one user that do the registration
    public function customer(){
        return $this->belongsTo(User::class, 'main_participant_id', 'id');
    }

    // a registration can have many participants
    public function participants(){
       return $this->hasMany('AppParticipant');
    }

    public function conference(){
        return $this->belongsTo('AppConference');
    }

    public function payment()
    {
        return $this->hasOne('AppPayment');
    }
}

Participant model:

class Participant extends Model
{

    // a participant belongs to a registration
    public function registration(){
        return $this->belongsTo('AppRegistration');
    }

    public function registration_type(){
        return $this->belongsTo('AppRegistrationType');
    }
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.