title: Laravel pivot table count in where clause of a scope method
body: <p>Following are my model's relationship:  </p>
  
  <p>Concerts  </p>
  
  <p>Customers  </p>
  
  <p>Venues belongs to concert (One to Many)  </p>
  
  <p>Timeslots belongs to venue (One to Many)  </p>
  
  <p>Tickets (Many to Many of Customers &amp; Timeslots)</p>
  
  <p>I'm trying to get the count of a many to many relationship of timeslot as part of a where clause in a scope method of my model Venue.</p>
  
  <pre><code>public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
  {
      $query-&gt;where('concert_id', $concert_id);
      $query-&gt;whereHas('timeslots', function ($query2) {
          $query2-&gt;where('slots', '&gt;=', "Ticket.counts"); 
      });
  }
  </code></pre>
  
  <p>How do I get the Ticket.counts value?</p>