Laravel5.2: How do I obtain the role of the currently logged-on user when 'zizaco/entrust' is used?

Source: Internet
Author: User
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.2How 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.2How 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.