From 83d7266d2b29f0909acdc456830a9b53a65b7536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Honor=C3=A9?= Date: Thu, 6 Jun 2024 10:55:51 +0200 Subject: [PATCH] Enhance efficiency of fetching latest activity --- README.md | 4 ++-- docs/advanced-usage/define-causer-for-runtime.md | 4 ++-- docs/advanced-usage/logging-model-events.md | 8 ++++---- docs/advanced-usage/manipulate-changes-array.md | 2 +- docs/advanced-usage/using-multiple-logs.md | 4 ++-- docs/advanced-usage/using-placeholders.md | 2 +- docs/basic-usage/logging-activity.md | 10 +++++----- docs/introduction.md | 4 ++-- tests/TestCase.php | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9a2b7887..853de100 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/docs/advanced-usage/define-causer-for-runtime.md b/docs/advanced-usage/define-causer-for-runtime.md index 120b7c71..d4dce09c 100644 --- a/docs/advanced-usage/define-causer-for-runtime.md +++ b/docs/advanced-usage/define-causer-for-runtime.md @@ -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 ``` diff --git a/docs/advanced-usage/logging-model-events.md b/docs/advanced-usage/logging-model-events.md index bfb19ea9..0ad386c3 100644 --- a/docs/advanced-usage/logging-model-events.md +++ b/docs/advanced-usage/logging-model-events.md @@ -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 @@ -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 @@ -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']]; @@ -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' ``` diff --git a/docs/advanced-usage/manipulate-changes-array.md b/docs/advanced-usage/manipulate-changes-array.md index 47a16e55..890f80d1 100644 --- a/docs/advanced-usage/manipulate-changes-array.md +++ b/docs/advanced-usage/manipulate-changes-array.md @@ -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', diff --git a/docs/advanced-usage/using-multiple-logs.md b/docs/advanced-usage/using-multiple-logs.md index 2ae93a2f..6e8bbc9c 100644 --- a/docs/advanced-usage/using-multiple-logs.md +++ b/docs/advanced-usage/using-multiple-logs.md @@ -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'; ``` @@ -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 diff --git a/docs/advanced-usage/using-placeholders.md b/docs/advanced-usage/using-placeholders.md index b943ce36..4df041c3 100644 --- a/docs/advanced-usage/using-placeholders.md +++ b/docs/advanced-usage/using-placeholders.md @@ -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'; ``` diff --git a/docs/basic-usage/logging-activity.md b/docs/basic-usage/logging-activity.md index d4bb2be9..495602b4 100644 --- a/docs/basic-usage/logging-activity.md +++ b/docs/basic-usage/logging-activity.md @@ -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'; ``` @@ -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`; ``` @@ -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`; ``` @@ -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' @@ -113,7 +113,7 @@ activity() }) ->log('edited'); -$lastActivity = Activity::all()->last(); +$lastActivity = Activity::latest()->first(); $lastActivity->my_custom_field; // returns 'my special value' ``` diff --git a/docs/introduction.md b/docs/introduction.md index 9fb062be..19f99af8 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -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 @@ -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 diff --git a/tests/TestCase.php b/tests/TestCase.php index f631a110..980d5f01 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -97,7 +97,7 @@ protected function seedModels(...$modelClasses) public function getLastActivity(): ?Activity { - return Activity::all()->last(); + return Activity::latest()->first(); } public function markTestAsPassed(): void