So I have two tables, one called members
and one called memberships
My goal is to count the number of members who have a certain membership. I've set up foreign keys and relationships are working fine, to the point where I need to do the counts.
My scope is (in Member
model)
public function scopeActive($query) {
return $query->where('membership_ended_at', Null);
}
My relationship is (in Member
model)
public function membership() {
return $this->belongsTo('App\Membership');
}
This query works fine, and I see how many members who are active()
that has the membership_id
of 6.
$members_student = Membership::find(6)->members()->active()->count();
I don't know if that's supposed to work, but it does. Now, the issue I have is that we have a regular student membership, and a student abroad membership with the ID of 14.
I assumed maybe this would work, but I quickly realized I was wrong
$members_student = Membership::find([6,14])->members()->active()->count();
I know I can call two queries and just add the two together, but I'm looking for a more elegant solution. Something that only required one query, and will half my queries.
Hopefully someone else has seen this before
Thanks to someone on laravel.io chat I managed to figure this one out. Posting it here in case anyone else is also looking for the answer.
The solution: