Update the member count column to exclude admins rather than include
This commit is contained in:
@@ -61,8 +61,7 @@ class Organisation extends Model
|
||||
*/
|
||||
protected $appends = [
|
||||
'member_count',
|
||||
'admin_count',
|
||||
'non_admin_count'
|
||||
'admin_count'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -99,28 +98,23 @@ class Organisation extends Model
|
||||
/**
|
||||
* Get a count of members within this organisation (excludes admins).
|
||||
*/
|
||||
public function getNonAdminCountAttribute()
|
||||
public function getTotalMemberCountAttribute()
|
||||
{
|
||||
return $this->members(true)->count();
|
||||
return $this->members()->count() + $this->administrators()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of members within this organisation.
|
||||
*/
|
||||
public function members($exclude_admins = false)
|
||||
public function members()
|
||||
{
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
$qry = $this
|
||||
return $this
|
||||
->belongsToMany($classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id')
|
||||
->where('flag_admin', false)
|
||||
->withTimestamps();
|
||||
|
||||
if ($exclude_admins) {
|
||||
return $qry->where('flag_admin', false);
|
||||
} else {
|
||||
return $qry;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,9 +126,7 @@ class Organisation extends Model
|
||||
$classMapper = static::$ci->classMapper;
|
||||
|
||||
return $this
|
||||
->belongsToMany(
|
||||
$classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id'
|
||||
)
|
||||
->belongsToMany($classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id')
|
||||
->where('flag_admin', true)
|
||||
->withTimestamps();
|
||||
}
|
||||
@@ -150,6 +142,7 @@ class Organisation extends Model
|
||||
{
|
||||
$memberCountsInner = DB::table('organisation_members')
|
||||
->selectRaw('organisation_id, COUNT(*) AS member_count')
|
||||
->where('flag_admin', false)
|
||||
->groupBy('organisation_id');
|
||||
$memberCounts = DB::table('organisations')
|
||||
->leftJoinSub($memberCountsInner, 'member_counts_inner', function ($join) {
|
||||
@@ -169,26 +162,12 @@ class Organisation extends Model
|
||||
->select('id AS organisation_id')
|
||||
->selectRaw('COALESCE(admin_count, 0) AS admin_count');
|
||||
|
||||
$nonAdminCountsInner = DB::table('organisation_members')
|
||||
->selectRaw('organisation_id, COUNT(*) AS non_admin_count')
|
||||
->where('flag_admin', false)
|
||||
->groupBy('organisation_id');
|
||||
$nonAdminCounts = DB::table('organisations')
|
||||
->leftJoinSub($nonAdminCountsInner, 'non_admin_counts_inner', function ($join) {
|
||||
$join->on('non_admin_counts_inner.organisation_id', '=', 'organisations.id');
|
||||
})
|
||||
->select('id AS organisation_id')
|
||||
->selectRaw('COALESCE(non_admin_count, 0) AS non_admin_count');
|
||||
|
||||
return $query
|
||||
->leftJoinSub($memberCounts, 'member_counts', function ($join) {
|
||||
$join->on('member_counts.organisation_id', '=', 'organisations.id');
|
||||
})
|
||||
->leftJoinSub($adminCounts, 'admin_counts', function ($join) {
|
||||
$join->on('admin_counts.organisation_id', '=', 'organisations.id');
|
||||
})
|
||||
->leftJoinSub($nonAdminCounts, 'non_admin_counts', function ($join) {
|
||||
$join->on('non_admin_counts.organisation_id', '=', 'organisations.id');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user