This is the documentation for
v1 but the latest version is
v4
.
You can switch versions in the menu on the left/at the top.
Check your current version with the following command:
composer show spatie/laravel-activitylog
##Description
This is the most basic way to log activity:
activity()->log('Look mum, I logged something');
You can retrieve the activity using the Spatie\Activitylog\Models\Activity
model.
$lastActivity = Activity::all()->last();
$lastActivity->description;
##Setting a subject
You can specify on which object the activity is performed by using performedOn
:
activity()
->performedOn($someContentModel)
->log('edited');
$lastActivity = Activity::all()->last();
$lastActivity->subject;
The performedOn
-function has a shorter alias name: on
##Setting a causer
You can set who or what caused the activity by using causedBy
:
activity()
->causedBy($userModel)
->performedOn($someContentModel)
->log('edited');
$lastActivity = Activity::all()->last();
$lastActivity->causer;
The causedBy()
-function has a shorter alias named: by
If you're not using causedBy
the package will automatically use the logged in user.
##Setting custom properties
You can add any property you want to an activity by using withProperties
activity()
->causedBy($userModel)
->performedOn($someContentModel)
->withProperties(['key' => 'value'])
->log('edited');
$lastActivity = Activity::all()->last();
$lastActivity->getExtraProperty('key')