php - Count two rows with scopes and relationships (Laravel) -
so have 2 tables, 1 called members
, 1 called memberships
my goal count number of members have membership. i've set foreign keys , relationships working fine, point need counts.
my scope (in member
model)
public function scopeactive($query) { return $query->where('membership_ended_at', null); }
my relationship (in member
model)
public function membership() { return $this->belongsto('app\membership'); }
this query works fine, , see how many members active()
has membership_id
of 6.
$members_student = membership::find(6)->members()->active()->count();
i don't know if that's supposed work, does. now, issue have have regular student membership, , student abroad membership id of 14.
i assumed maybe work, realized wrong
$members_student = membership::find([6,14])->members()->active()->count();
i know can call 2 queries , add 2 together, i'm looking more elegant solution. required 1 query, , half queries.
hopefully else has seen before
thanks on laravel.io chat managed figure 1 out. posting here in case else looking answer.
the solution:
$members_student = member::wherein('membership_id', [6,14])->active()->count();
Comments
Post a Comment