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:
@@ -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');
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user