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

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -