Example #1
0
 private function recountForums()
 {
     $this->info('Recounting forum counters...');
     // We're calling the model directly to avoid caching issues
     $forums = Forum::all();
     foreach ($forums as $forum) {
         // We need the topics later to calculate the post number and the last post
         $topics = Topic::where('forum_id', '=', $forum->id)->orderBy('created_at', 'desc');
         $forum->num_topics = $topics->count();
         $numPosts = 0;
         $lastPost = null;
         $lastPostUser = null;
         foreach ($topics->get() as $topic) {
             $numPosts += $topic->num_posts;
             // We can simply override this variable all the time.
             // The topics are sorted so the last time we override this we have our last post
             $lastPost = $topic->last_post_id;
             $lastPostUser = $topic->lastPost->user_id;
         }
         $forum->num_posts = $numPosts;
         $forum->last_post_id = $lastPost;
         $forum->last_post_user_id = $lastPostUser;
         $forum->save();
     }
     // Override our old cache to populate our new numbers
     $this->cache->forever('forums.all', $forums);
     // We could also recache this cache but the recount tool is already busy
     // so probably better to leave it to the first user
     $this->cache->forget('forums.index_tree');
     $this->info('Done' . PHP_EOL);
 }
Example #2
0
 /**
  * Update the last post for this forum
  *
  * @param Forum $forum The forum to update
  * @param Post  $post
  *
  * @return mixed
  */
 public function updateLastPost(Forum $forum, Post $post = null)
 {
     if ($post === null) {
         $topic = $forum->topics->sortByDesc('last_post_id')->first();
         if ($topic != null) {
             $post = $topic->lastPost;
         }
     }
     if ($post != null) {
         $forum->update(['last_post_id' => $post->id, 'last_post_user_id' => $post->user_id]);
     } else {
         $forum->update(['last_post_id' => null, 'last_post_user_id' => null]);
     }
     return $forum;
 }