Hello and thank you for creating this package, its been great help for us.
We have been running the activity log for few months and now noticed that we dont have any logging for pivot data.
For example we have:
class LocationRelation extends Model
{
use LogsActivity;
protected static $logAttributes = ['*'];
}
We use ->attach() ->detach() ->sync() to add/remove/sync the data.
Are we doing something wrong or do we need to manually log these changes?
Hey,
this was already requested several times. The key point is that your pivot model needs a PK and the pivot model has to set $incrementing = true.
As suggested in one of these issues: it would be a good idea to add it to the docs.
I would be happy to review a PR that adds it and merge it in.
Would you be available to work on one?
Hello this might be useful if anyone is still looking for an answer.
Laravel Relationship Events is a package by Viacheslav Ostrovskiy that adds extra model relationship events. This package comes with the following traits that are used to register listeners on a model鈥檚 boot() method
Relationship Events
For all getting here: that's the open task in question.
As suggested in one of these issues: it would be a good idea to add it to the docs.
I would be happy to review a PR that adds it and merge it in.
— https://github.com/spatie/laravel-activitylog/issues/703#issuecomment-602535620
Hi,
I needed to track the relation Role<->Permissions on the Spatie Permission package (https://github.com/spatie/laravel-permission).
So following the different threads I came up with this boot() method being set in the Role model and using the package: https://github.com/chelout/laravel-relationship-events.
public static function boot() {
parent::boot();
static::belongsToManySyncing(function (string $relation, Role $role) {
/* Only track sync of permissions */
if ($relation === 'permissions') {
/* Mimic the logic used on `updating` event */
$role->oldAttributes = static::logChanges((new static)->setRawAttributes($role->getOriginal()));
}
});
static::belongsToManySynced(function (string $relation, Role $role) {
/* Only track sync of permissions */
if ($relation === 'permissions') {
/* Trigger 'updated' event to trigger existing diff logic */
$attrs = $role->attributeValuesToBeLogged('updated');
if ($role->isLogEmpty($attrs) && !$role->shouldSubmitEmptyLogs()) {
return;
}
/* Clean the relation: Extract names from permissions */
$attrs = collect($attrs)
->map(fn(array $element) => collect($element)
->map(fn($attribute) => ($attribute instanceof Collection) ? $attribute->pluck('name') : $attribute))
->toArray();
/* Save the diff */
app(ActivityLogger::class)
->useLog($role->getLogNameToUse())
->performedOn($role)
->withProperties($attrs)
->log($role->getDescriptionForEvent("updated:sync.{$relation}"));
}
});
}
This will generate an activity log with the followings changes when syncing permissions A and B to role someRole which had no permissions:
[
"attributes" => [
'name' =>'someRole',
'guard_name' => 'admin',
'permissions' => ['A', 'B']
],
"old" => [
'name' =>'someRole',
'guard_name' => 'admin',
'permissions' => []
]
]
Good luck.
So I need to use the 3rd party package to support detaching? I am correct?
Most helpful comment
Hello this might be useful if anyone is still looking for an answer.