php - Retrieving number of answers in each category -
i have these tables :
- questions
- answers
- categories
i related questions table answers :
question > hasmany > answers
how can retrieve number of answers in each category without relating categories table answers ?
i wrote code , works should :
@php $a_num = 0; if ($category->questions->count() > 0) { foreach ($category->questions $question) { $a_num += $question->answers->count(); } } @endphp
is there better way of doing ?
Comments
Post a Comment