/** * Refreshes the thread's meta infos * * @return void */ public function refresh() { $forumPost = ForumPost::whereThreadId($this->id)->orderBy('created_at', 'desc')->firstOrFail(); $postsCount = ForumPost::whereThreadId($this->id)->count(); $this->posts_count = $postsCount; $this->updated_at = $forumPost->updated_at; $this->forceSave(); }
/** * Deletes a thread * * @param int The id of the thread */ public function delete($id) { $forumPost = ForumPost::isAccessible()->findOrFail($id); $forumThread = ForumThread::findOrFail($id); $forum = $forumThread->forum; ForumPost::whereThreadId($forumThread->id)->delete(); /* * Updates the users posts counter */ $query = DB::table('forum_posts')->whereThreadId(DB::raw($forumThread->id))->groupBy('creator_id')->select('creator_id', DB::raw('COUNT(creator_id) AS count'))->toSql(); DB::table('users')->join(DB::raw('(' . $query . ') AS sq'), 'id', '=', 'creator_id')->update(['posts_count' => DB::raw('posts_count - count')]); $forumThread->delete(); $forum->refresh(); }