Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically

1.2k Views Asked by At

Hi I am using laravel Sentinel as my Auth, also I am trying to use laravel auditing I am getting "Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically".

In my user model I have added a static function resolveId() for adding user_id in Laravel Auditing 'audits' table

public static function resolveId(){
    return Sentinel::getUser()->getUserId();
    //return auth()->check() ? auth()->user()->getAuthIdentifier() : null;
}

When I try to use \Sentinel::getUser() I am getting the error below.

Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically
3

There are 3 best solutions below

0
Alexey Mezenin On BEST ANSWER

From the docs:

After installing the package, open your Laravel config file located at config/app.php and add the following lines. In the $aliases array add the following facades for this package.

'Sentinel' => Cartalyst\Sentinel\Laravel\Facades\Sentinel::class,

Then just add this to the top of the class:

use Sentinel;
2
Nikola Gavric On

Put this use on top of the file in question:

use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
0
Quetzy Garcia On

I'm aware that the package version @manikandan k was asking help for is 4.x or 5.x, and while the documentation does mention the use case for Sentinel, it doesn't provide an actual example.

Since version 6.x, the Audit Resolvers documentation has this very use case, where Sentinel is used instead.

I suggest updating the resolver logic to the following:

return Sentinel::check() ? Sentinel::getUser()->getUserId() : null;

This will prevent calling getUserId() on null, when a user isn't logged.