避免表中的重复响应-Laravel 5.6

时间:2018-07-11 13:17:37

标签: php laravel

我有一个模板,该模板显示两个问题,供3个(三个)客户回答。如下面的响应所示,它重复了问题以显示每个客户的答案。

但是我想拥有的是,将类似的答案与它们的数量归为一组。我该怎么办?感谢您的帮助。

模板问题

1. How would you describe our products and services?

2. Overall, how satisfied or dissatisfied are your with our company?

现在,当客户回答问题时,它在我的视图中显示为

响应

                        Question                          Answers

 How would you describe our products and services?     -  Reliable

 How would you describe our products and services?     -  Expensive

 How would you describe our products and services?     -  Reliable



   Overall, how satisfied or dissatisfied are your with - Satisfied
   our company?

   Overall, how satisfied or dissatisfied are your with - Satisfied
   our company?

   Overall, how satisfied or dissatisfied are your with - Dissatisfied
   our company?

我想要的

                        Question                          Answers

 How would you describe our products and services?     -  Reliable  (2)
                                                          Expensive (1)

 Overall, how satisfied or dissatisfied are your with - Satisfied    (2)
   our company?                                       - Dissatisfied (1)

控制器

 public function view_survey_answers(Survey $survey) 
  {
     $survey->load('user.questions.answers');
    return view('answer.ans', compact('survey'));
  }

answer.ans

   <table class="bordered striped">
  <thead>
    <tr>
        <th data-field="id">Question</th>
        <th  data-field="ans">Answer(s)</th>
    </tr>
  </thead>

  <tbody>
    @forelse ($survey->questions as $item)
    @foreach ($item->answers as $answer)
    <tr>
      <td width="30%">{{ $item->title }}</td>                
     <td  width="40%" >{{$answer->answer}}</td>    
    </tr>
    @endforeach    
    @empty
      <tr>
        <td>
          No answers provided by you for this Survey
        </td>
        <td></td>
      </tr>
    @endforelse
  </tbody>
</table>

问题

class Question extends Model
{
  protected $casts = [
      'option_name' => 'array',
  ];
  protected $fillable = ['title', 'question_type', 'option_name', 'user_id'];
  public function survey() {
    return $this->belongsTo(Survey::class);
  }

  public function user() {
    return $this->belongsTo(User::class);
  }

  public function answers() {
    return $this->hasMany(Answer::class);
  }

  public function count_answers() 
  {    
    return $this->select(‘answers’, DB::raw('count(*) as counter'))->answers()->groupBy(‘answers’)->get();  

  }

  protected $table = 'question';

}

答案

use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
    protected $fillable = ['answers'];
    protected $table = 'answer';

    public function survey() {
      return $this->belongsTo(Survey::class);
    }

    public function question() {
      return $this->belongsTo(Question::class);
    }

    public function customers(){
      return $this->belongsTo(Customer::class, 'customer_phone','phone');
     }
}

1 个答案:

答案 0 :(得分:0)

  1. 在您的Question模型中引入新功能

    public function count_answers() {    
        return $this—>select(‘answers’, DB::raw('count(*) as counter'))->answers()->groupBy(‘answers’)->get();  
    }
    
  2. 编辑视图

 <table class="bordered striped">
  <thead>
    <tr>
        <th data-field="id">Question</th>
        <th  data-field="ans">Answer(s)</th>
    </tr>
  </thead>

  <tbody>
    @forelse ($survey->questions as $item)
    <tr>
      <td width="30%">{{ $item->title }}</td>
<td  width="40%" >                
    @foreach ($item->count_answers as $answer)
     <div>{{$answer->answer}} ({{$answer->counter}})</div>
    @endforeach    
</td>
    </tr>
    @empty
      <tr>
        <td>
          No answers provided by you for this Survey
        </td>
        <td></td>
      </tr>
    @endforelse
  </tbody>
</table>