Search code examples
phplaravelpermissionsrolesentrust

Laravel Entrust -> get all Permissions for a role?


Is it possible to get all permissions which are assigned to a specific role? Because in my Role and Permission Models are no relations defined and I don't know if its safe to add them by myself.


Solution

  • You have to assign the relations between your models in order to get permissions of a specific role

    Role Class:

    class Role extends EntrustRole{
    
        public function permissions(){
           return $this->belongsToMany(Permission::class);
        }
    
    }
    

    and now you can get all permissions related to a role like this:

     $user->load('roles.permissions');
     $permissions = $user->roles->first()->permissions;