2
votes

I have the code below in a view to show info about all registrations in a specific congress: the name and surname of the user that did the registration, etc.

Then there is also the link "Details". When "Details" is clicked it appears a modal that shows the registration details:

  • the user that did the registration
  • the name and surname of each registered participant(s) in that clicked registration

My doubt is how to show also besides this info above show also the question(s) and the answer(s) to this questions.

To explain better the questions context: for example, the user John is doing a registration in the congress "congress test" and for exmaple this congress has two ticket types associated: tt1 and tt2. And both ticket types (tt1 and tt2) have the custom question "Whats your phone?" associated to them. So in the "congress test" registration form this questions will appear for each selected ticket and John need to answer to the question "Whats your phone?" for each ticket.

So in the code below I also want to show the answers to the custom questions that the user answered in the registration form, but Im not understanding how. Do you know what is necessary to get the answers? Is necessary a 1 to many relationship between Participant and Answer?

code working without showing the answers of the custom questions:

@foreach($congress->registrations as $registration)
    <tr>
        <td>{{$registration->customer->name}} {{$registration->customer->surname}}</td>
        <td>...</td>
        <td>
            <a class="btn btn-primary showDetails" 
             data-regid="{{$registration->id}}">Details</a>
        </td>
    </tr>

    <div class="modal fade" id="registrationDetails-{{ $registration->id }}" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-body">
                    <dl>
                        <dt>Name</dt>
                        <dd>{{ $registration->customer->name }}</dd>
                        <dt>Email</dt>
                        <dd>{{ $registration->customer->email }}</dd>
                    </dl>

                  @foreach ($registration->participants as $participant)
                      <dl>
                          <dt>Name</dt>
                          <dd>{{ $participant->name }}</dd>
                          <dt>Surname</dt>
                          <dd>{{ $participant->surname }}</dd>
                          <dt>Ticket Type</dt>
                          <dd>{{ $participant->registration_type->name }}</dd>

                           <!-- Doubt -->

                           <dt>Doubt: How to get the 
                             custom question(s)? (ex: Whats your phone?)</dt>
                          <dd> Doubt: how to get the answer?</dd>

                      </dl>
                  @endforeach
        </div>
    </div>
@endforeach

Context Diagram: enter image description here

Laravel Models:

class Congress extends Model
{ 
    // A congress has one creator
    public function creator(){
        return $this->belongsTo('App\User', 'user_id');
    }
    public function ticketTypes(){
        return $this->hasMany('App\TicketType', 'congress_id');
    }
    public function registrations(){
        return $this->hasMany('App\Registration', 'congress_id');
    }
}

// User model
class User extends Authenticatable
{
    public function congresses(){
        return $this->hasMany('App\Congresss', 'user_id');
    }

    // A user can register in many congresses
    public function registrations(){
        return $this->hasMany('App\Registration','main_participant_id');
    }
}

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

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

    public function congress(){
        return $this->belongsTo('App\Congress');
    }
}

// Participant Model
class Participant extends Model
{
    // a participant belongs to a registration
    public function registration(){
        return $this->belongsTo('App\Registration');
    }
     public function ticket_type(){
        return $this->belongsTo('App\TicketType');
    }
}

// Ticket Type model
class TicketType extends Model
{
    public function congress(){
        return $this->belongsTo('App\Congress');
    }

    public function questions(){
        return $this->belongsToMany('App\Question', 'ticket_type_questions')->withPivot(['required']);;
    }

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

// Question model
class Question extends Model
{
    public function ticket_type(){
        return $this->belongsToMany('App\TicketType', 'ticket_type_questions')
            ->withPivot('required');
    }
}

// Answer model
class Answer extends Model
{
    public function question(){
        return $this->belongsTo('Question');
    }
    public function participant(){
        return $this->belongsTo('Participant');
    }
}

// TicketTypeQuestion model
class TicketTypeQuestion extends Model
{
}

DB:

enter image description here

1

1 Answers

0
votes

Yeah, as you surmised, one way is to add a one-to-many relation from participant to answers (since you already have the DB column for participant_id, anyway).

class Participant extends Model
{
    // a participant belongs to a registration
    public function registration(){
        return $this->belongsTo('App\Registration');
    }
     public function ticket_type(){
        return $this->belongsTo('App\TicketType');
    }
    public function answers(){
        return $this->hasMany('App\Answer');
    }
}

Then, in your view, you can do something like:

@foreach($participant->answers as $answer)
    <p>{{ $answer->question->question }}: {{ $answer->answer }}</p>
@endforeach

If you're not already, I would make sure you are eager-loading all of this before calling the view. E.g. $congress->load('registrations.participants.answers.question')