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

Enhance efficiency of fetching latest activity #1310

Closed
wants to merge 1 commit into from
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
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