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 de633c53..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)) { + if ($model->isCustomDateTimeCast($cast) || $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() {