如何检查用户是管理员还是不在Laravel刀片视图中 [英] How to check user is Admin or not in laravel blade view
本文介绍了如何检查用户是管理员还是不在Laravel刀片视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
用户表如
+==============+
| User |
+==============+
| id |
+--------------+
| firstname |
+--------------+
| lastname |
+--------------+
| email |
+--------------+
| password |
+--------------+
和我的角色表
+==============+
| Roles |
+==============+
| id |
+--------------+
| name |
+--------------+
我的role_user表是
and my role_user table is
+=============+
| role_user |
+=============+
| user_id |
+-------------+
| role_id |
+-------------+
我如何查看当前登录的用户是admin或普通用户?
How can I check current logged user is admin or normal user?
推荐答案
Role.php
Role.php
use Illuminate\Database\Eloquent\Model;
class Role extends Model {
protected $fillable = [
'name'
];
/**
* A role can have many users.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users() {
return $this->belongsToMany('App\User');
}
}
然后,您可以将其添加到用户模型:
Then you can add this to User model:
public function isAdmin()
{
foreach ($this->roles()->get() as $role)
{
if ($role->name == 'Admin')
{
return true;
}
}
}
strong>
View
@if(Auth::check())
@if (Auth::user()->isAdmin())
<h2>Admin user enter code here<h2>
@endif
@endif
这篇关于如何检查用户是管理员还是不在Laravel刀片视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文