Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trait to return a models last activity as a relation #1213

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/advanced-usage/logging-model-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,21 @@ final class PivotModel extends Pivot
```

After these changes you can log activities on your pivot models as expected.

## Getting the last activity on the model

You can get the last activity for the model as a relation by using `with(['lastActivity'])` to eager load the activity.

```php
$newsItem = NewsItem::with(['lastActivity'])->first();

$newsItem->lastActivity // Activity model
```

You can also retrieve the causer with the last activity.

```php
$newsItem = NewsItem::with(['lastActivity.causer'])->first();

$newsItem->lastActivity->causer // Causer model
```
8 changes: 8 additions & 0 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateInterval;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -122,6 +123,13 @@ public function activities(): MorphMany
return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject');
}

public function lastActivity(): MorphOne
{
return $this->activities()
->one()
->latestOfMany();
}

public function getDescriptionForEvent(string $eventName): string
{
if (! empty($this->activitylogOptions->descriptionForEvent)) {
Expand Down
37 changes: 37 additions & 0 deletions tests/LastActivityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Spatie\Activitylog\Test\Models\ArticleWithLastActivity;

it('gets the last activity for the model', function () {
$article = ArticleWithLastActivity::first();
$article->name = 'Title change';
$article->save();

$article->load('lastActivity');

expect($article->lastActivity)
->description->toBe('updated')
->subject_type->toBe(ArticleWithLastActivity::class);

expect($article->lastActivity->changes->toArray())
->toEqual(
[
'attributes' => [
'name' => 'Title change',
],
'old' => [
'name' => 'name 1',
],
],
);
});

it('gets the created activity for the model', function () {
$article = ArticleWithLastActivity::create(['name' => 'New article']);

$createdArticle = ArticleWithLastActivity::with(['lastActivity'])->find($article->id);

expect($createdArticle->lastActivity)
->description->toBe('created')
->subject_type->toBe(ArticleWithLastActivity::class);
});
17 changes: 17 additions & 0 deletions tests/Models/ArticleWithLastActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\Activitylog\Test\Models;

use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class ArticleWithLastActivity extends Article
{
use LogsActivity;

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['name']);
}
}