Skip to content

Commit

Permalink
Merge pull request #688 from spatie/fix-issue-680
Browse files Browse the repository at this point in the history
Fix [Laravel 7] json_decode error when updating a json-field
  • Loading branch information
Gummibeer committed Mar 10, 2020
2 parents f6c33e2 + c7580e6 commit 4e44214
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

All notable changes to `spatie/laravel-activitylog` will be documented in this file

## 3.11.2 - 2020-03-10

- fix Laravel 7 array/json casted attributes [#680](https://github.com/spatie/laravel-activitylog/pull/680)

## 3.11.1 - 2020-03-02

- fix requirements
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -38,6 +38,7 @@
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^4.0|^5.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
Expand Down
8 changes: 7 additions & 1 deletion src/Traits/DetectsChanges.php
Expand Up @@ -18,7 +18,13 @@ protected static function bootDetectsChanges()

//temporary hold the original attributes on the model
//as we'll need these in the updating event
$oldValues = (new static)->setRawAttributes($model->getOriginal());
if (method_exists(Model::class, 'getRawOriginal')) {
// Laravel >7.0
$oldValues = (new static)->setRawAttributes($model->getRawOriginal());
} else {
// Laravel <7.0
$oldValues = (new static)->setRawAttributes($model->getOriginal());
}

$model->oldAttributes = static::logChanges($oldValues);
});
Expand Down
68 changes: 67 additions & 1 deletion tests/DetectsChangesTest.php
Expand Up @@ -396,7 +396,7 @@ public function it_will_store_the_values_when_deleting_the_model_with_softdelete
}

/** @test */
public function it_can_store_the_changes_of_array_casted_properties()
public function it_can_store_the_changes_of_collection_casted_properties()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['json'];
Expand Down Expand Up @@ -428,6 +428,72 @@ public function it_can_store_the_changes_of_array_casted_properties()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_of_array_casted_properties()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['json'];
public static $logOnlyDirty = true;
protected $casts = ['json' => 'array'];

use LogsActivity;
};

$article = $articleClass::create([
'json' => ['value' => 'original'],
]);

$article->json = collect(['value' => 'updated']);
$article->save();

$expectedChanges = [
'attributes' => [
'json' => [
'value' => 'updated',
],
],
'old' => [
'json' => [
'value' => 'original',
],
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_of_json_casted_properties()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['json'];
public static $logOnlyDirty = true;
protected $casts = ['json' => 'json'];

use LogsActivity;
};

$article = $articleClass::create([
'json' => ['value' => 'original'],
]);

$article->json = collect(['value' => 'updated']);
$article->save();

$expectedChanges = [
'attributes' => [
'json' => [
'value' => 'updated',
],
],
'old' => [
'json' => [
'value' => 'original',
],
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_use_nothing_as_loggable_attributes()
{
Expand Down

0 comments on commit 4e44214

Please sign in to comment.