Skip to content

Commit

Permalink
fix for logging changes attributes when deleting soft deletable models
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Sep 28, 2017
1 parent ae0de19 commit ee927e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 17 additions & 11 deletions src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,7 +41,7 @@ public function attributesToBeLogged(): array

public function shouldlogOnlyDirty(): bool
{
if (! isset(static::$logOnlyDirty)) {
if (!isset(static::$logOnlyDirty)) {
return false;
}

Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit ee927e8

Please sign in to comment.