Reputation: 33
Following are my model's relationship:
Concerts
Customers
Venues belongs to concert (One to Many)
Timeslots belongs to venue (One to Many)
Tickets (Many to Many of Customers & Timeslots)
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.
public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
{
$query->where('concert_id', $concert_id);
$query->whereHas('timeslots', function ($query2) {
$query2->where('slots', '>=', "Ticket.counts");
});
}
How do I get the Ticket.counts value?
Upvotes: 0
Views: 231
Reputation: 33
Got the answer myself.
public function scopeGetConcertVenuesThatHaveSlotsRemains($query, $concert_id)
{
$query->where('concert_id', $concert_id);
$query->whereHas('timeslots', function ($query2) {
$query2->withCount('customers')->whereColumn('slots', '>', 'customers_count');
});
}
Upvotes: 2