So I have Subject
, that contains Stack
, and stacks contains Question
. And with every question, there is answers, and I would like to count the answers on every Stack
, but I'm stuck.
This is what I'm trying to do, one of many solutions I have been fiddling with:
$subjects = Subject::with(['stack.question', 'stack.answersCount'])->get();
return $subjects;
The Subject
model has the following relation
public function stack(){
return $this->hasMany(Stack::class);
}
And if we take a look on the Stack
model on how the relations are build:
public function question(){
return $this->hasMany(Question::class);
}
public function answers(){
return $this->hasMany(Answers::class);
}
public function answersCount(){
return $this->answers()->selectRaw('answer, count(*) as aggregate')->groupBy('answer');
}
But when I return $subjects
, answer_count is empty.
"answers_count": []
Its very confusing, because when I run the same query in sql, I get results.
select answer, count(*) as aggregate from `answers` where `answers`.`stack_id` in ('1', '6') group by `answer`
correct 9
wrong 4
So how can I count all answers? And group them?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2fIvjBx
via IFTTT
Aucun commentaire:
Enregistrer un commentaire