lundi 10 juin 2019

Laravel polymorphic relationship complex selection

I have users that have a polymorphic relations to different entities. Here's my data structure:

group
    -id
    -title
region
   -id
   -title
activity
   -id
   -title

users
   -id
   -name

relationships
    -id
    -user_id
    -relationship_id
    -relationship_type
    -relationship_level

The relationships are defined in all models, for example - User model:

public function organization()
{
    return $this->morphedByMany('App\Organization', 'relationship')->withPivot('relationship_level');
}  

The relationship_level column holds one of "Administrator", "Regional Manager", "Group Manager", "User" etc... You can be multiple roles in multiple entities.

I want to write a scope in the Users model scopeByAutorization that returns all users below the Auth::user().

For example if I'm a group manager in one group and regional manager in another region I want the query to return everyone who has a relationship_level lower than mine (if they go User->Group M->Regional M) in those respective group and region.

Is that achievable at all with Eloquent or do I need to write lots of custom logic?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire