From ef849b534f892ec7f659b7e3618d5017d0b3f8db Mon Sep 17 00:00:00 2001 From: Dennis Koch Date: Tue, 5 Oct 2021 17:56:12 +0200 Subject: [PATCH 1/2] Add support for immutable_date casts --- src/Traits/LogsActivity.php | 2 +- tests/DetectsChangesTest.php | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index de633c53..e311aed9 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -350,7 +350,7 @@ public static function logChanges(Model $model): array if ($model->hasCast($attribute)) { $cast = $model->getCasts()[$attribute]; - if ($model->isCustomDateTimeCast($cast)) { + if ($model->isCustomDateTimeCast($cast) || (method_exists($model, 'isImmutableCustomDateTimeCast') && $model->isImmutableCustomDateTimeCast($cast))) { $changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]); } } diff --git a/tests/DetectsChangesTest.php b/tests/DetectsChangesTest.php index c3070773..44bf3c28 100644 --- a/tests/DetectsChangesTest.php +++ b/tests/DetectsChangesTest.php @@ -1782,6 +1782,44 @@ public function getActivitylogOptions(): LogOptions $this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray()); } + /** @test */ + public function it_can_use_custom_immutable_date_cast_as_loggable_attributes() + { + $userClass = new class() extends User { + use LogsActivity; + + protected $fillable = ['name', 'text']; + protected $casts = [ + 'created_at' => 'immutable_date:d.m.Y', + ]; + + public function getActivitylogOptions(): LogOptions + { + return LogOptions::defaults() + ->logAll(); + } + }; + + Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0)); + $user = new $userClass(); + $user->name = 'my name'; + $user->text = 'my text'; + $user->save(); + + $expectedChanges = [ + 'attributes' => [ + 'id' => $user->getKey(), + 'name' => 'my name', + 'text' => 'my text', + 'created_at' => '01.01.2017', + 'updated_at' => '2017-01-01T12:00:00.000000Z', + 'deleted_at' => null, + ], + ]; + + $this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray()); + } + /** @test */ public function it_can_store_the_changes_of_json_attributes() { From 867731be060bbdcdac31610962a413f3d5c05386 Mon Sep 17 00:00:00 2001 From: Dennis Koch Date: Tue, 5 Oct 2021 19:04:03 +0200 Subject: [PATCH 2/2] Bump laravel/database version --- composer.json | 2 +- src/Traits/LogsActivity.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c2c9616f..423704f1 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "require": { "php": "^8.0", "illuminate/config": "^8.0", - "illuminate/database": "^8.0", + "illuminate/database": "^8.53", "illuminate/support": "^8.0", "spatie/laravel-package-tools": "^1.6.3" }, diff --git a/src/Traits/LogsActivity.php b/src/Traits/LogsActivity.php index e311aed9..dafa1ab4 100644 --- a/src/Traits/LogsActivity.php +++ b/src/Traits/LogsActivity.php @@ -350,7 +350,7 @@ public static function logChanges(Model $model): array if ($model->hasCast($attribute)) { $cast = $model->getCasts()[$attribute]; - if ($model->isCustomDateTimeCast($cast) || (method_exists($model, 'isImmutableCustomDateTimeCast') && $model->isImmutableCustomDateTimeCast($cast))) { + if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) { $changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]); } }