[英]How find role name and id of a specific user id who is not logged in using Sentinel in laravel
How to find role name and id of a specific user id who is not logged in using Sentinel in Laravel? 如何查找未在Laravel中使用Sentinel登录的特定用户ID的角色名称和ID?
// **** I have used this code =>
<?php Sentinel::getUser(4)->inRole('admin'); ?>
But this doesn't work as Sentinel::getUser() works only for logged in user. 但这不起作用,因为Sentinel :: getUser()仅适用于登录用户。 But I need to find role name and role id of nonlogged user in one of my managament , so what should be the script.
但是我需要在其中之一中找到未登录用户的角色名称和角色ID,因此脚本应该是什么。 I need help on this.
我需要帮助。
You can get any user based on their id by calling $user = User::find(4)
. 您可以通过调用
$user = User::find(4)
获得基于其ID的任何用户。 Then with sentinel you should be able to call $roles = $user->roles
which will return all the roles that are assigned to that user or call $user->inRole('admin')
to check if they're in a specific role. 然后使用哨兵,您应该可以调用
$roles = $user->roles
,它将返回分配给该用户的所有角色,或者调用$user->inRole('admin')
来检查它们是否在特定角色中角色。
EDIT 编辑
For this to work you will need to have a User model class set up in the app directory that extends the base Sentinel EloquentUser class. 为此,您需要在app目录中设置一个User模型类,以扩展Sentinel EloquentUser基类。 The top of the file should look something like this:
文件顶部应如下所示:
<?php
namespace App;
use Cartalyst\Sentinel\Users\EloquentUser;
use Sentinel;
class User extends EloquentUser {
Hi I have got a solution to my problem , and its working in the view page , the code that i have used is given below 嗨,我有一个解决我的问题的方法,它在视图页面中有效,下面给出了我使用的代码
<?php
$user_id=3;
$userR = App\User::find($user_id);
$chk=$userR->inRole('admin');
if($chk)
{
//***** logic comes here ******
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.