Permalink
Browse files

fix for logging changes attributes when deleting soft deletable models

  • Loading branch information...
freekmurze committed Sep 28, 2017
1 parent ae0de19 commit ee927e8035110254f0f40f2d33da87e2382af759
Showing with 20 additions and 11 deletions.
  1. +3 −0 CHANGELOG.md
  2. +17 −11 src/Traits/DetectsChanges.php
View
@@ -2,6 +2,9 @@
All notable changes to `spatie/laravel-activitylog` will be documented in this file
## 2.1.2 - 2017-09-28
- fix for logging changes attributes when deleting soft deletable models
## 2.1.1 - 2017-09-12
- make sure `properties` always is a collection
@@ -3,6 +3,7 @@
namespace Spatie\Activitylog\Traits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Exceptions\CouldNotLogChanges;
trait DetectsChanges
@@ -40,7 +41,7 @@ public function attributesToBeLogged(): array
public function shouldlogOnlyDirty(): bool
{
if (! isset(static::$logOnlyDirty)) {
if (!isset(static::$logOnlyDirty)) {
return false;
}
@@ -49,12 +50,15 @@ public function shouldlogOnlyDirty(): bool
public function attributeValuesToBeLogged(string $processingEvent): array
{
if (! count($this->attributesToBeLogged())) {
if (!count($this->attributesToBeLogged())) {
return [];
}
$tmp = $this;
$properties['attributes'] = static::logChanges($this->exists ? $tmp->fresh() ?? $this : $this);
$properties['attributes'] = static::logChanges(
$this->exists
? $this->fresh() ?? $this
: $this
);
if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') {
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null);
@@ -64,13 +68,15 @@ public function attributeValuesToBeLogged(string $processingEvent): array
if ($this->shouldlogOnlyDirty() && isset($properties['old'])) {
$properties['attributes'] = array_udiff_assoc(
$properties['attributes'],
$properties['old'],
function ($new, $old) {
return $new <=> $old;
}
);
$properties['old'] = collect($properties['old'])->only(array_keys($properties['attributes']))->all();
$properties['attributes'],
$properties['old'],
function ($new, $old) {
return $new <=> $old;
}
);
$properties['old'] = collect($properties['old'])
->only(array_keys($properties['attributes']))
->all();
}
return $properties;

0 comments on commit ee927e8

Please sign in to comment.