简体   繁体   English

如何查找未在laravel中使用Sentinel登录的特定用户ID的角色名称和ID

[英]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.

相关问题 如何在moodle中使用用户ID找到用户角色 - How can I find the user role using user id in moodle 如何使用 laravel 6 中的 session 从数据库中获取登录的用户 ID - How to get logged user id from database using session in laravel 6 如何在 Laravel 5.2 中获取登录的用户 ID - How to Get the logged in User ID in Laravel 5.2 Laravel:获取登录用户的 ID - Laravel : Get id of logged user 如何使用附加 function 将用户 ID 和角色 ID 附加到 laravel 中的 pivot 表? - How can I attach the user id and role id to the pivot table in laravel using attach function? Laravel-查找特定ID - Laravel - find specific ID laravel使用雄辩的身份验证获取当前登录用户的ID - laravel get the id of the currently logged in user using Eloquent auth 如何使用 Laravel 5.1 使用更新记录的用户 ID 填充 modified_by? - How to populate the modified_by with the user_id of who made the update to the record using laravel 5.1? 如何在社交引擎中获取当前用户(已登录)的user_id? - How to get the current user's (who is logged in) user_id in social engine? 如何在不使用 session 函数的情况下获取特定登录用户的 ID,仅 CodeIgniter 中的 API 方法 - How to get the ID of the specific logged in user without using session functions, ONLY API method in CodeIgniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM