title: Laravel , getting count of an attribute
body: <p>I have a table called Vote, and another table called sub_item. Vote and sub_item share a one to many relationship. I have another table called Vote_casts, which records which sub item the user cast a vote on. 
  This is an example of it:</p>
  
  <p>Now, I want to calculate what the number of votes on each sub_item for a particular vote is. I am not storing this value, as it would denormalise the table.
  Instead, I wish to calculate this data from the vote_casts table. 
  (by calculating the count of the rows for a particular sub_item_id, i will be able to do so).</p>
  
  <p>So in my Model, I created this :</p>
  
  <pre><code>public function getCountAttribute(){
  
      return Vote_cast_action::where('sub_item_id',$this-&gt;id)-&gt;count();
  
  }
  </code></pre>
  
  <p>This works as expected. But the problem is, for each sub item in a vote, the query is called. So if there are 20 sub items in a vote, this line itself will be called 20 times (I am saying this after looking at my sql query log)</p>
  
  <p>Is there a way to make this query more efficient in laravel ?
  <a href="https://i.stack.imgur.com/QZzlf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QZzlf.png" alt="enter image description here"></a></p>