The current version is laravel5.2. how can I obtain the role of the currently logged-on user when using this package and zizaco entrust5.2? {Code...} the current version is laravel 5.2 in use and
Zizaco/entrust 5.2
How can I obtain the role of the currently logged-on user during this package?
Namespace App \ Services; use App \ User; use Zizaco \ Entrust \ EntrustRole; use Illuminate \ Support \ Facades \ Cache; class NameAndRole {public $ username; public $ role; public function _ construct () {$ user = \ Auth: user (); $ this-> username = $ user-> name; $ role =; // How do I obtain the role of the current logon user? }}
Reply content:
The current version is laravel 5.2 in use andZizaco/entrust 5.2
How can I obtain the role of the currently logged-on user during this package?
Namespace App \ Services; use App \ User; use Zizaco \ Entrust \ EntrustRole; use Illuminate \ Support \ Facades \ Cache; class NameAndRole {public $ username; public $ role; public function _ construct () {$ user = \ Auth: user (); $ this-> username = $ user-> name; $ role =; // How do I obtain the role of the current logon user? }}
In your users model and roles model, there should be methods like the following:
Class User extends Model {//... public function roles () {// multi-to-many relationship (a user has multiple roles) return $ this-> belongsToMany (\ App \ Role: class );} //...} class Role extends Model {//... public function users () {// many-to-many relationship (a role has granted multiple users) return $ this-> belongsToMany (\ App \ User: class );} //...}
The above method is provided in the model. the way to obtain a role is as follows (access dynamic attributes ):
$ Roles = $ user-> roles; // A user may have multiple roles.