diff --git a/CHANGELOG.md b/CHANGELOG.md index 49fc6136..e4e854e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to `laravel-permission` will be documented in this file +## 2.5.1 - 2017-09-02 +- add getRoleNames() method to return a collection of assigned roles + ## 2.5.0 - 2017-08-30 - add compatiblity with Laravel 5.5 diff --git a/README.md b/README.md index 41977917..0839bca5 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,7 @@ $permissions = $user->permissions; $permissions = $user->getAllPermissions(); // get a collection of all defined roles -$roles = $user->roles->pluck('name'); // Returns a collection +$roles = $user->getRoleNames(); // Returns a collection ``` The `HasRoles` trait also adds a scope to your models to scope the query to certain roles: diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index 4244da52..a72ffb68 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -311,6 +311,11 @@ public function getAllPermissions(): Collection ->values(); } + public function getRoleNames(): Collection + { + return $this->roles->pluck('name'); + } + protected function getStoredRole($role): Role { if (is_string($role)) { diff --git a/tests/HasRolesTest.php b/tests/HasRolesTest.php index 1fccf4dd..8f846a11 100644 --- a/tests/HasRolesTest.php +++ b/tests/HasRolesTest.php @@ -420,4 +420,15 @@ public function it_can_list_all_the_coupled_permissions_both_directly_and_via_ro $this->testUser->getAllPermissions()->pluck('name') ); } + + /** @test */ + public function it_can_retrieve_role_names() + { + $this->testUser->assignRole('testRole', 'testRole2'); + + $this->assertEquals( + collect(['testRole', 'testRole2']), + $this->testUser->getRoleNames() + ); + } }