Add a scope to the organisation model to include if the currentUser is a member or admin of which organisations (null indicates not a member nor an admin)

This commit is contained in:
2022-02-10 17:18:06 +00:00
parent f2c6677dc1
commit c3e7c24e6d

View File

@@ -253,4 +253,23 @@ class Organisation extends Model implements OrganisationInterface
->where('user_id', $userId); ->where('user_id', $userId);
}); });
} }
/**
* Query scope to get all organisations but indicate which the specified user is a member of.
*
* @param Builder $query
* @param int $userId
*
* @return Builder
*/
public function scopeWithUser($query, $userId)
{
$membersInner = DB::table('organisation_members')
->selectRaw('organisation_id, 1 AS is_member, flag_admin AS is_admin')
->where('user_id', $userId);
return $query
->leftJoinSub($membersInner, 'user_orgs', function ($join) {
$join->on('user_orgs.organisation_id', '=', 'organisations.id');
});
}
} }