Skip to content

Commit

Permalink
Enhance efficiency of fetching latest activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Honoré committed Jun 6, 2024
1 parent 3f20032 commit 83d7266
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ activity()
->withProperties(['customProperty' => 'customValue'])
->log('Look, I logged something');

$lastLoggedActivity = Activity::all()->last();
$lastLoggedActivity = Activity::latest()->first();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
Expand All @@ -46,7 +46,7 @@ $newsItem->name = 'updated name';
$newsItem->save();

//updating the newsItem will cause the logging of an activity
$activity = Activity::all()->last();
$activity = Activity::latest()->first();

$activity->description; //returns 'updated'
$activity->subject; //returns the instance of NewsItem that was saved
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced-usage/define-causer-for-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ CauserResolver::setCauser($causer);

$product->update(['name' => 'New name']);

Activity::all()->last()->causer; // Product Model
Activity::all()->last()->causer->id; // Product#1 Owner
Activity::latest()->first()->causer; // Product Model
Activity::latest()->first()->causer->id; // Product#1 Owner

```

Expand Down
8 changes: 4 additions & 4 deletions docs/advanced-usage/logging-model-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ $newsItem = NewsItem::create([
]);

//creating the newsItem will cause an activity being logged
$activity = Activity::all()->last();
$activity = Activity::latest()->first();

$activity->description; //returns 'created'
$activity->subject; //returns the instance of NewsItem that was created
Expand All @@ -68,7 +68,7 @@ $newsItem->name = 'updated name';
$newsItem->save();

//updating the newsItem will cause an activity being logged
$activity = Activity::all()->last();
$activity = Activity::latest()->first();

$activity->description; //returns 'updated'
$activity->subject; //returns the instance of NewsItem that was created
Expand Down Expand Up @@ -97,7 +97,7 @@ Now, what happens when you call delete?
$newsItem->delete();

//deleting the newsItem will cause an activity being logged
$activity = Activity::all()->last();
$activity = Activity::latest()->first();

$activity->description; //returns 'deleted'
$activity->changes; //returns ['attributes' => ['name' => 'updated name', 'text' => 'Lorum']];
Expand Down Expand Up @@ -153,7 +153,7 @@ $newsItem = NewsItem::create([
]);

//creating the newsItem will cause an activity being logged
$activity = Activity::all()->last();
$activity = Activity::latest()->first();

$activity->description; //returns 'This model has been created'
```
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced-usage/manipulate-changes-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ NewsItem::addLogChange(new RemoveKeyFromLogChangesPipe('name'));
$article = NewsItem::create(['name' => 'new article', 'text' => 'new article text']);
$article->update(['name' => 'update article', 'text' => 'update article text']);

Activity::all()->last()->changes();
Activity::latest()->first()->changes();
/*
'attributes' => [
'text' => 'updated text',
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced-usage/using-multiple-logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Without specifying a log name the activities will be logged on the default log.
```php
activity()->log('hi');

$lastActivity = Spatie\Activitylog\Models\Activity::all()->last();
$lastActivity = Spatie\Activitylog\Models\Activity::latest()->first();

$lastActivity->log_name; //returns 'default';
```
Expand All @@ -24,7 +24,7 @@ You can specify the log on which an activity must be logged by passing the log n
```php
activity('other-log')->log("hi");

Activity::all()->last()->log_name; //returns 'other-log';
Activity::latest()->first()->log_name; //returns 'other-log';
```

## Specifying a log for each model
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced-usage/using-placeholders.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ activity()
->withProperties(['laravel' => 'awesome'])
->log('The subject name is :subject.name, the causer name is :causer.name and Laravel is :properties.laravel');

$lastActivity = Activity::all()->last();
$lastActivity = Activity::latest()->first();
$lastActivity->description; //returns 'The subject name is article name, the causer name is user name and Laravel is awesome';
```
10 changes: 5 additions & 5 deletions docs/basic-usage/logging-activity.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ activity()->log('Look mum, I logged something');
You can retrieve the activity using the `Spatie\Activitylog\Models\Activity` model.

```php
$lastActivity = Activity::all()->last(); //returns the last logged activity
$lastActivity = Activity::latest()->first(); //returns the last logged activity

$lastActivity->description; //returns 'Look mum, I logged something';
```
Expand All @@ -28,7 +28,7 @@ activity()
->performedOn($someContentModel)
->log('edited');

$lastActivity = Activity::all()->last(); //returns the last logged activity
$lastActivity = Activity::latest()->first(); //returns the last logged activity

$lastActivity->subject; //returns the model that was passed to `performedOn`;
```
Expand All @@ -45,7 +45,7 @@ activity()
->performedOn($someContentModel)
->log('edited');

$lastActivity = Activity::all()->last(); //returns the last logged activity
$lastActivity = Activity::latest()->first(); //returns the last logged activity

$lastActivity->causer; //returns the model that was passed to `causedBy`;
```
Expand All @@ -67,7 +67,7 @@ activity()
->withProperties(['key' => 'value'])
->log('edited');

$lastActivity = Activity::all()->last(); //returns the last logged activity
$lastActivity = Activity::latest()->first(); //returns the last logged activity

$lastActivity->getExtraProperty('key'); //returns 'value'

Expand Down Expand Up @@ -113,7 +113,7 @@ activity()
})
->log('edited');

$lastActivity = Activity::all()->last();
$lastActivity = Activity::latest()->first();

$lastActivity->my_custom_field; // returns 'my special value'
```
4 changes: 2 additions & 2 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ activity()
->withProperties(['customProperty' => 'customValue'])
->log('Look mum, I logged something');

$lastLoggedActivity = Activity::all()->last();
$lastLoggedActivity = Activity::latest()->first();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
Expand All @@ -41,7 +41,7 @@ $newsItem->name = 'updated name';
$newsItem->save();

//updating the newsItem will cause an activity being logged
$activity = Activity::all()->last();
$activity = Activity::latest()->first();

$activity->description; //returns 'updated'
$activity->subject; //returns the instance of NewsItem that was created
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function seedModels(...$modelClasses)

public function getLastActivity(): ?Activity
{
return Activity::all()->last();
return Activity::latest()->first();
}

public function markTestAsPassed(): void
Expand Down

0 comments on commit 83d7266

Please sign in to comment.