diff --git a/src/Traits/DetectsChanges.php b/src/Traits/DetectsChanges.php index 75e7cfa6..2ed8436e 100644 --- a/src/Traits/DetectsChanges.php +++ b/src/Traits/DetectsChanges.php @@ -91,9 +91,13 @@ public function attributeValuesToBeLogged(string $processingEvent): array } $properties['attributes'] = static::logChanges( - $this->exists - ? $this->fresh() ?? $this - : $this + $processingEvent == 'retrieved' + ? $this + : ( + $this->exists + ? $this->fresh() ?? $this + : $this + ) ); if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { diff --git a/tests/LogsActivityTest.php b/tests/LogsActivityTest.php index b6100c1e..41847936 100644 --- a/tests/LogsActivityTest.php +++ b/tests/LogsActivityTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Collection; use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Test\Models\Article; +use Spatie\Activitylog\Test\Models\Issue733; use Spatie\Activitylog\Test\Models\User; use Spatie\Activitylog\Traits\LogsActivity; @@ -423,6 +424,21 @@ public function it_will_submit_a_log_with_json_changes() $this->assertSame($expectedChanges, $changes); } + /** @test */ + public function it_will_log_the_retrieval_of_the_model() + { + $article = Issue733::create(['name' => 'my name']); + + $retrieved = Issue733::whereKey($article->getKey())->first(); + $this->assertTrue($article->is($retrieved)); + + $activity = $this->getLastActivity(); + + $this->assertInstanceOf(get_class($article), $activity->subject); + $this->assertTrue($article->is($activity->subject)); + $this->assertEquals('retrieved', $activity->description); + } + public function loginWithFakeUser() { $user = new $this->user(); diff --git a/tests/Models/Issue733.php b/tests/Models/Issue733.php new file mode 100644 index 00000000..e2cab372 --- /dev/null +++ b/tests/Models/Issue733.php @@ -0,0 +1,18 @@ +