Skip to content

Commit f4705d4

Browse files
authored
Merge pull request #969 from pxlrbt/master
Add support for immutable_date casts
2 parents 7028cba + 867731b commit f4705d4

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"require": {
3434
"php": "^8.0",
3535
"illuminate/config": "^8.0",
36-
"illuminate/database": "^8.0",
36+
"illuminate/database": "^8.53",
3737
"illuminate/support": "^8.0",
3838
"spatie/laravel-package-tools": "^1.6.3"
3939
},

src/Traits/LogsActivity.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public static function logChanges(Model $model): array
350350
if ($model->hasCast($attribute)) {
351351
$cast = $model->getCasts()[$attribute];
352352

353-
if ($model->isCustomDateTimeCast($cast)) {
353+
if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) {
354354
$changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]);
355355
}
356356
}

tests/DetectsChangesTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,44 @@ public function getActivitylogOptions(): LogOptions
17821782
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
17831783
}
17841784

1785+
/** @test */
1786+
public function it_can_use_custom_immutable_date_cast_as_loggable_attributes()
1787+
{
1788+
$userClass = new class() extends User {
1789+
use LogsActivity;
1790+
1791+
protected $fillable = ['name', 'text'];
1792+
protected $casts = [
1793+
'created_at' => 'immutable_date:d.m.Y',
1794+
];
1795+
1796+
public function getActivitylogOptions(): LogOptions
1797+
{
1798+
return LogOptions::defaults()
1799+
->logAll();
1800+
}
1801+
};
1802+
1803+
Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
1804+
$user = new $userClass();
1805+
$user->name = 'my name';
1806+
$user->text = 'my text';
1807+
$user->save();
1808+
1809+
$expectedChanges = [
1810+
'attributes' => [
1811+
'id' => $user->getKey(),
1812+
'name' => 'my name',
1813+
'text' => 'my text',
1814+
'created_at' => '01.01.2017',
1815+
'updated_at' => '2017-01-01T12:00:00.000000Z',
1816+
'deleted_at' => null,
1817+
],
1818+
];
1819+
1820+
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
1821+
}
1822+
17851823
/** @test */
17861824
public function it_can_store_the_changes_of_json_attributes()
17871825
{

0 commit comments

Comments
 (0)