1
votes

I'm using zizaco/entrust package in my laravel project beside multi Auth package name Hesto/multi-auth

Our project on laravel 5.4,

i get below error when i want to get current logged in users' role and permissions with this method: Entrust::hasRole('role-name'); OR Auth::user()->hasRole('role-name');

But I can access users' Role with this method for example :

$user = User::find($userid);
dd($user->hasRole('admin')); // Return true 

i followed exactly installation instruction but i get below error :

`Non-static method Zizaco\Entrust\Entrust::hasRole() should not be called statically`

How can i solve my problem, Thanks in advance

1
Check the use statement. Are you using the class or the facade? You want to be using the facade.sushibrain
You might be using the wrong class. Have you tried to use \Entrust::hasRole();. Not yet tested, but the solution is that you need to use EntrustFacade mentioned over here : github.com/Zizaco/entrust/blob/master/src/Entrust/…PaladiN
@PaladiN it's work for me , and i had another problem in getting current logged in users in vendor/zizaco/entrust/src/Entrust/Entrust.php in user() method. because im using Hesto/MultiAuth the previous return couldn't retrive current user ,Then for fix the problem , I changed return $this->app->auth->user(); to return Auth::guard('user')->user(); but i think still there is a problem with using Entrust FecadeHamed Yarandi

1 Answers

2
votes

In this error message you have answer to your problem:

Non-static method Zizaco\Entrust\Entrust::hasRole() should not be called statically

You called this method hasRole() statically, but this method is non static. It means that you need to create object of this class, but you instead that used a class.

In the example that you gave:

$user = User::find($userid);
dd($user->hasRole('admin')); // Return true

you create an object of class User, and class User (I think) implements class Entrust:

$user = User::find($userid);

$user is an object and it`s not static, you can use hasRole().

In other words, to use method hasRole() (literally - is someone has a role?) you need this someone:

$user->hasRole('admin') // Is this user has role `admin`?

Hope, I explained it. Sorry about my English (I'm just studying).