I have template which displays two questions for my 3 (three)clients to respond to. As seen in the Response below, it duplicates the question to show each and every clients answer.
But what i want to have is, group similar answers below with their count. How can i do this please? Thanks for your help in advance.
Template Question
1. How would you describe our products and services?
2. Overall, how satisfied or dissatisfied are your with our company?
Now when clients respond to the questions it is shown in my view like
Response
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?
What i want
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)
Controller
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>
Question
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';
}
Answer
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');
}
}
Introduce a new function in your Question model
public function count_answers() { return $this—>select(‘answers’, DB::raw('count(*) as counter'))->answers()->groupBy(‘answers’)->get(); }
Edit the view
<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>
</div>