Laravel-activitylog: Logging model events Causer

Created on 16 Jan 2018  路  6Comments  路  Source: spatie/laravel-activitylog

Hi!
I am using ActivityLog for logging user activity on models, I used the LogsActivity trait on a model (say Article) and put CausesActivity on the users model (that in my case is called Person).

Logging works but I don't get any Causer int he db, is there a way I can do this?

Thanks!
C.

Most helpful comment

Is there a way to set the causer for seeders? Or other automated jobs? I guess technically they're not logged in so the causer isn't known from the Guard?

All 6 comments

If there is a user logged in, it should automatically be used as the causer.

To set a causer manually use the causedBy function.

activity()
   ->causedBy($user)
   ->log('Look mum, I logged something');

Make sure the call to log is used last in the chain.

Thanks for your answer.
I understand (from the docs) how to add a causer with a manual log, but I would like it to work with the model event and it doesn't seem to work.

The user is logged in and I am modifying the model through a REST API call (authenticated with the api token guard). I have user credentials in the api, the modification of the model is ok and I have the activity logged but without the User in the causer fields... any hint where I should look into?

Thanks again.

Ok solved thanks!
During my tests I changed the driver in the config to "web" and I set it back to the default:

'default_auth_driver' => null,

And it works.

I had two guards so I created a middleware for both and dynamically set the default auth driver.

public function handle($request, Closure $next, $guard = 'admin')
{
    config()->set('activitylog.default_auth_driver', $guard);

    return $next($request);
}

Works for me.

Is there a way to set the causer for seeders? Or other automated jobs? I guess technically they're not logged in so the causer isn't known from the Guard?

Was this page helpful?
0 / 5 - 0 ratings