我正在建立一个应用程序,显示调查结果的信息。在每次调查中,我都有疑问,每个问题都有他的答案。
但是我在构建表时遇到了一些问题,标题表和正文是dynamacly构建的,标题是问题,正文是答案。
检查我的代码表:
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
@foreach($survey->answers->groupBy('email')->first() as $answer)
<th>{{$answer->question->internal_name}}</th>
@endforeach
</tr>
</thead>
<tbody>
@foreach($survey->answers->groupBy('email') as $answer)
<tr>
@foreach($answer as $a)
<td>{{$a->answer}}</td>
@endforeach
</tr>
@endforeach
</tbody>
<tfoot>
@foreach($survey->answers->groupBy('email')->first() as $answer)
<th>{{$answer->question->internal_name}}</th>
@endforeach
</tfoot>
</table>
我遇到的唯一问题是标题标题问题之间的同步化是答案,有时问题是如果问题按不同的顺序排序,例如不在正确的位置。
不能弄清楚我做错了什么,上面我离开了桌子,也许有人可以知道我做错了什么。
Tables:
surveys:
- id;
- name;
questions:
- id;
- survey_id;
- label;
- internal_name;
answers:
- id;
- survey_id;
- question_id;
- answer
- email;
问题模型:
class Question extends Model
{
public function survey()
{
return $this->belongsTo(Survey::class);
}
public function answers() {
return $this->hasMany(Answer::class);
}
public function oneAnswer(){
return $this->hasOne(Answer::class);
}
}
答案型号:
class Answer extends Model
{
public function survey() {
return $this->belongsTo(Survey::class);
}
public function question() {
return $this->belongsTo(Question::class);
}
}
答案 0 :(得分:0)
根据我的理解,请按question_id
进行订购。还注意到你要查询三次,我建议查询一次&amp;将其存储在变量中并使用它。
@php
$results = $survey->answers->groupBy('email')->orderBy('question_id');
@endphp
...
@foreach($results->first() as $answer)
...