Laravel 5与委托 - hasRole不工作 [英] Laravel 5 with entrust - hasRole not working

查看:1986
本文介绍了Laravel 5与委托 - hasRole不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录的用户和$ C $这里C工作:

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

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

我使用zizaco /委托和它的所有工作。我创建角色和权限,并给予我的用户有管理权限的管理角色。

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.

那么,为什么不这项工作:

So why doesn't this work:

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

如果我的print_r的$用户,我可以看到auth用户加载的,但hasRole不工作。我只是此刻的刀片模板中测试这一点,但尝试在具有相同的结果控制器。我的错误是:

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:

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

我的用户模型:

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'];

}

更新

我意识到hasRole工作对我来说,当我抬头看用户这样(返回应用程序\\型号\\用户对象):

I realized hasRole works for me when I look up the user this way (returns 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 ):

$user = \Auth::user();

我宁愿会想到它会工作的其他方式,当我拉起应用程序\\用户我想有机会获得hasRole但不一定当我搜索用户。我以为hasRole会工作从验证用户::()权,而无需与用户模式来查找用户... ???

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...???

推荐答案

发现问题。

在配置/ auth.php我不得不

in config/auth.php I had

'model' => 'App\User',

我的空间要求

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

这篇关于Laravel 5与委托 - hasRole不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆