3 months ago

User relationships with different results based on User role

Posted 3 months ago by stefano.curnis

Hey everyone.

I know this isn't the best solution and I should use Services or Repositories. Let's say I have a User and Areas. Every User is related to one or more Areas, but if this User is a super administrator, then it's related to all Areas.

public function manageableAreas()
        if ($this->hasRole("Super Admin")){
            return Area::query();
        return $this->belongsToMany('App\Models\Area')

This works when I call $user->manageableAreas()->get(). This won't work if I call $user->manageableAreas or I try to eager load the relation count with ->withCount('manageableAreas')

How can I implement this feature?

Thanks guys

Please sign in or create an account to participate in this conversation.