I have 2 tables as below:
Subscription
table have a entrance_limit
column, and it has a hasMany('Attendance')
relationship. The entrance_limit
column will actually limit the number of rows in Attendance
. For example, if the entrance_limit
value is 10, then we only can create 10 rows in Attendance
.
Attendance
table has a belongsTo('Subscription')
relationship.
How do I get the list of Subscription
that have total number of Attendance
less than the entrance_limit
value?
Subscription::whereHas('attendances', function($q) {
## something like this
$q->count() < subscription->entrance_limit
})
Finally able to get it working with the following:
Subscription::whereRaw('
(select count(*) from `attendances` where `attendances`.`subscription_id` = `subscriptions`.`id`) < `subscriptions`.`entrance_limit`
');
Model your query has below
Subscription::whereHas('attendances', function($q) {
$q->where('entrance_limit','>',$q->count());
})