我在下面的代码中显示了特定国会中所有注册的信息:进行注册的用户的姓名和姓氏等。
然后还有“详细信息”链接。单击“详细信息”时,会显示一个显示注册详细信息的模式:
我的疑问是如何显示除上述信息之外还显示了问题和问题的答案。
更好地解释问题上下文:例如,用户John正在国会“国会测试”中进行注册,例如,这个国会有两种相关的票证类型:tt1和tt2。两种票类型(tt1和tt2)都有自定义问题“什么是你的手机?”与他们相关联。因此,在“国会测试”登记表中,每个选定的机票都会显示这些问题,约翰需要回答“你的电话是什么?”这个问题。每张票。
因此,在下面的代码中,我还想显示用户在注册表单中回答的自定义问题的答案,但我不明白如何。你知道什么是得到答案的必要吗?参与者和答案之间是否需要1对多的关系?
代码工作时没有显示自定义问题的答案:
@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
上下文图: set_fact module
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:
答案 0 :(得分:0)
是的,正如你推测的那样,一种方法是从参与者到答案添加一对多的关系(因为你已经拥有了Particip_id的DB列,无论如何)。
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');
}
}
然后,在您看来,您可以执行以下操作:
@foreach($participant->answers as $answer)
<p>{{ $answer->question->question }}: {{ $answer->answer }}</p>
@endforeach
如果你还没有,我会在调用视图之前确保你eager-loading所有这些。例如。 $congress->load('registrations.participants.answers.question')