1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Laravel 5.2:使用`Zizaco/entrust`时 怎么获得当前登录用户的角色?

Laravel 5.2:使用`Zizaco/entrust`时 怎么获得当前登录用户的角色?

时间:2020-04-05 02:29:58

相关推荐

Laravel 5.2:使用`Zizaco/entrust`时 怎么获得当前登录用户的角色?

后端开发|php教程

php,laravel

后端开发-php教程

当前使用的版本是laravel 5.2 在使用和Zizaco/entrust 5.2这个包的时候,怎么获得当前登录用户的角色?

仿豆瓣同城 源码,ubuntu修改机器名,数据搜索爬虫兼职,php quote(),英文编辑seolzw

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 = ; //怎么获得当前登录用户的角色? }}

帝国软件公司源码,ubuntu系统手动分区,爬虫爬取限制,番禺php培训,西宁网页seolzw

回复内容:

网站防护系统源码,ubuntu安装界面教程,python爬虫图片讲解,php访问加速,福建seo机构lzw

当前使用的版本是laravel 5.2 在使用和Zizaco/entrust 5.2这个包的时候,怎么获得当前登录用户的角色?

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 = ; //怎么获得当前登录用户的角色? }}

在你的users模型以及roles模型里,应该有像下边这样的方法:

class User extends Model{ //... public function roles () { // 多对多的关系(一个用户有多个角色) return $this->belongsToMany(\App\Role::class);} //...}class Role extends Model{ //... public function users () { // 多对多的关系(一个角色赋予了多个用户) return $this->belongsToMany(\App\User::class); } //...}

模型里边有了以上方法,获取角色就像下边这样(访问动态属性):

$roles = $user->roles; // 一个用户可能有多个角色

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。