Search code examples
phplaravel-5.3

How to change the Auth Guard in Laravel Auditing?


I have used the Laravel Auditing plugin (http://www.laravel-auditing.com/docs/3.1) to log the all models changes.Am using different auth system but the Laravel Auditing

getLoggedInUserId()

using laravel core one so need to change that. i have forked this plugin and edited the function directly its worked. But i like to find another ways if you have idea share with me ?

 protected function getLoggedInUserId()
    {
        try {
            if (Auth::check()) {
                return Auth::user()->getAuthIdentifier();
            }
        } catch (\Exception $e) {
            return;
        }
    }

Solution

  • Unfortunately, until version 4 of the package, you couldn't change the user resolver without modifying the actual code.

    However, from version 4 onwards, you can do so in the configuration file (config/audit.php).

    The user resolver can be set in two ways.

    As a Closure: return [ 'user' = [ 'resolver' => function () { return Auth::check() ? Auth::user()->getAuthIdentifier() : null; }, ], ];

    As a FQCN: return [ 'user' = [ 'resolver' => App\User::class, ], ];

    TIP: You have to implement the OwenIt\Auditing\Contracts\UserResolver interface in the App\User class for this to work.

    See the full documentation here.