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); }
/** * Get all forums. * * @return mixed */ public function all() { return $this->forumModel->all(); }