manikandan k
manikandan k

Reputation: 33

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

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

Upvotes: 2

Views: 1305

Answers (3)

Quetzy Garcia
Quetzy Garcia

Reputation: 1840

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.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

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;

Upvotes: 3

Nikola Gavric
Nikola Gavric

Reputation: 3543

Put this use on top of the file in question:

use Cartalyst\Sentinel\Laravel\Facades\Sentinel;

Upvotes: 0

Related Questions