title: Laravel scope model count from relationship
body: <p>I am trying to count unread messages from conversation. This is my table structure.</p>
  
  <pre><code>mysql&gt; describe conversations;
  +-------------+------------------+------+-----+---------+----------------+
  | Field       | Type             | Null | Key | Default | Extra          |
  +-------------+------------------+------+-----+---------+----------------+
  | id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |                             |
  | other fields which are not needed            
  +-------------+------------------+------+-----+---------+----------------+
  6 rows in set (0,00 sec)
  
  mysql&gt; describe messages;
  +-----------------+------------------+------+-----+-------------------+-----------------------------+
  | Field           | Type             | Null | Key | Default           | Extra                       |
  +-----------------+------------------+------+-----+-------------------+-----------------------------+
  | id              | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
  | is_seen         | tinyint(1)       | NO   |     | NULL              |                             |                         |
  | conversation_id | int(10) unsigned | NO   | MUL | NULL              |                             |
  +-----------------+------------------+------+-----+-------------------+-----------------------------+
  </code></pre>
  
  <p>Relationships between tables are working fine :</p>
  
  <pre><code>//Conversation.php 
  // this return all messages from the conversation
  public function messages(){
          return $this-&gt;hasMany('App\Message','conversation_id');
      }
  
  
  // I got stuck in this function
  public function scopeUnread($query){
          return $query-&gt;whereHas('messages', function($q){
              $q-&gt;where('is_seen',0);
          });
      }
  </code></pre>
  
  <p><strong>I am trying to count all unread messages from conversation, I did the function above, so I can get number of unread from conversation like this</strong></p>
  
  <pre><code>Conversation::find(1)-&gt;unread();
  </code></pre>