Don't save in the model

This commit is contained in:
Toby Zerner 2018-07-21 21:28:44 +09:30
parent d9cfc0e1b2
commit 73da82ebc0
2 changed files with 10 additions and 4 deletions

View File

@ -766,7 +766,6 @@ class User extends AbstractModel
public function refreshCommentsCount()
{
$this->comments_count = $this->posts()->count();
$this->save();
return $this;
}
@ -779,7 +778,6 @@ class User extends AbstractModel
public function refreshDiscussionsCount()
{
$this->discussions_count = $this->discussions()->count();
$this->save();
return $this;
}

View File

@ -66,11 +66,19 @@ class UserMetadataUpdater
private function updateCommentsCount(Post $post)
{
optional($post->user)->refreshCommentsCount();
$user = $post->user;
if ($user && $user->exists) {
$user->refreshCommentsCount()->save();
}
}
private function updateDiscussionsCount(Discussion $discussion)
{
optional($discussion->startUser)->refreshDiscussionsCount();
$user = $discussion->startUser;
if ($user && $user->exists) {
$user->refreshDiscussionsCount()->save();
}
}
}