2017-03-23 35 views
0

我建立一個應用程序的調查結果的顯示信息。在每次調查中,我都有問題,每個問題都有他的答案。循環和建立一個動態數據表

但即時通訊在構建表時遇到了一些問題,頭表和主體都是動態構建的,頭是問題,正文是答案。

Examle我的代碼表:

<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> 

,即時通訊有唯一的問題就是標題標籤的問題之間及以上的同步化是問題的答案,有時如果一個問題是排序在diferent爲了回答,例如不在正確的位置。

不知道我做錯了什麼,上面我離開桌子,也許有人可以有一個想法,即時做錯了什麼。

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); 
    } 

} 

截圖 enter image description here

回答

0

根據我的瞭解,通過question_id做的訂單。也注意到你正在查詢三次,我建議查詢一次&將它存儲在一個變量中並使用它。

@php 
    $results = $survey->answers->groupBy('email')->orderBy('question_id'); 
@endphp 
... 
@foreach($results->first() as $answer) 
...