带有委托的 Laravel 5 - hasRole 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28934449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:05:42  来源:igfitidea点击:

Laravel 5 with entrust - hasRole not working

authenticationlaravellaravel-5

提问by Dylan Glockler

I have a logged in user and the code here works:

我有一个登录用户,这里的代码有效:

@if( Auth::check() )
Logged in as: {{ Auth::user()->firstname }} {{ Auth::user()->lastname  }}
@endif

I'm using zizaco/entrust and it's all working. I've created roles and permissions and given my user the admin role which has administrative permission.

我正在使用 zizaco/entrust 并且一切正常。我已经创建了角色和权限,并为我的用户授予了具有管理权限的 admin 角色。

So why doesn't this work:

那么为什么这不起作用:

$user = Auth::user();
print_r($user->hasRole('admin'));

If I print_r the $user I can see that the Auth user is loaded, but hasRole is not working. I was just testing this within the blade template at the moment but tried it in the Controller with the same result. My error is:

如果我 print_r $user 我可以看到 Auth 用户已加载,但 hasRole 不起作用。我当时只是在刀片模板中测试它,但在控制器中尝试它并得到相同的结果。我的错误是:

BadMethodCallException in Builder.php line 1992:
Call to undefined method Illuminate\Database\Query\Builder::hasRole()

My user model:

我的用户模型:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Model {
  use EntrustUserTrait;

   protected $table = 'users';
   public $timestamps = true;

   use SoftDeletes;

   protected $dates = ['deleted_at'];

}

UPDATE

更新

I realized hasRole works for me when I look up the user this way (returns App\Models\User Object):

当我以这种方式查找用户时,我意识到 hasRole 对我有用(返回 App\Models\User Object):

$user = \App\Models\User::find( \Auth::user()->id );

but NOT when I find the user this way (returns App\User Object ):

但不是当我以这种方式找到用户时(返回 App\User Object ):

$user = \Auth::user();

I rather would have thought it would work the other way, when I pull up the App\User I'd have access to hasRole but not necessarily when I search for a user. I thought hasRole would work right from the Auth::user() without having to lookup the user with the user model...???

我宁愿认为它会以另一种方式工作,当我拉起 App\User 时,我可以访问 hasRole 但不一定在我搜索用户时。我认为 hasRole 可以直接从 Auth::user() 工作,而不必使用用户模型查找用户......???

回答by Dylan Glockler

Found the issue..

发现问题..

in config/auth.php I had

在 config/auth.php 我有

'model' => 'App\User',

My namespace requires

我的命名空间需要

'model' => 'App\Models\User',

回答by Jorge Luis Jiménez

Thanks for you update, it was a problem for me as well. And I used your advice and I put in my HomeController

感谢您的更新,这对我来说也是一个问题。我使用了你的建议并放入了我的 HomeController

public function index()
{
    $user = User::find( \Auth::user()->id );

    return view('home', compact('user'));
}

and then in my Home View

然后在我的主页视图中

@if ($user->hasRole('Admin'))
    <li><a href="/users/create">Create User</a></li>
@endif
<li><a href="/users">User List</a></li>
<li><a href="">Create Client</a></li>
<li><a href="">Create Payment</a></li>

and everything works perfect...

一切都很完美......