Php 如何获得每个参与者自定义问题的答案?(雄辩)
我有下面的代码,以显示有关特定会议中所有注册的信息:注册用户的姓名等 然后还有“详细信息”链接。单击“详细信息”时,会出现一个模式,显示注册详细信息:Php 如何获得每个参与者自定义问题的答案?(雄辩),php,laravel,eloquent,Php,Laravel,Eloquent,我有下面的代码,以显示有关特定会议中所有注册的信息:注册用户的姓名等 然后还有“详细信息”链接。单击“详细信息”时,会出现一个模式,显示注册详细信息: 进行注册的用户 该注册中每个注册参与者的姓名 我的疑问是,除了上述信息外,如何显示这些问题以及这些问题的答案 为了更好地解释问题上下文:例如,用户John正在大会“大会测试”中注册,对于exmaple,此大会有两种相关票证类型:tt1和tt2。两种票证类型(tt1和tt2)都有与之相关联的自定义问题“What's your phone?”。因
- 进行注册的用户
- 该注册中每个注册参与者的姓名
@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
DB:
是的,正如您所猜测的,一种方法是添加参与者与答案之间的一对多关系(因为您已经有了参与者id的DB列)
类参与者扩展模型
{
//参与者属于注册机构
公共职能注册(){
返回$this->belongsTo('App\Registration');
}
公共功能票证类型(){
返回$this->belongsTo('App\TicketType');
}
公共职能部门答覆(){
返回$this->hasMany('App\Answer');
}
}
然后,在您看来,您可以执行以下操作:
@foreach($participant->answers as $answer)
<p>{{ $answer->question->question }}: {{ $answer->answer }}</p>
@endforeach
@foreach($participant->答案为$answer)
{{$answer->question->question}}:{{{$answer->answer}
@endforeach
如果您还没有,我会在调用视图之前确保您已经完成了所有这些。例如,
$congress->load('registrations.participants.answers.question')
谢谢,但在视图中会出现一个错误“类”问题“not found”。你知道为什么吗?我没有“$congress->load('registrations.participants.answers.questions')”可能就是因为这个。索引方法只是“公共函数索引($id){$congression=congression::find($id);返回视图('participants.index')->with('congression',$congression);}”如果急于加载,索引方法应该保持“公共函数索引($id){$congression=congression::find($id);$event->load('registrations.participants.answers.questions'));返回视图('participants.index')->带有('congress',$congress);}“?对于这样的index(),出现了“调用[App\Answer]模型上的未定义关系[questions]”错误。我注意到,在您的代码中,在您的Answers模型上添加关系时,您没有引用完全命名空间的问题类。它应该是$this->belongsTo('App\Question')代码>(对于参与者也是如此。)我想这就是你的错误的来源。对不起,我在那条急切的加载建议中输入了一个错误。它应该是$congress->load('registrations.participants.answers.question')
(现已修复)
@foreach($participant->answers as $answer)
<p>{{ $answer->question->question }}: {{ $answer->answer }}</p>
@endforeach