where('status', $status); } if ($keyword) { $query->where('content', 'like', "%{$keyword}%"); } $paginator = $query->order('create_at', 'desc') ->paginate(['list_rows' => $limit, 'page' => $page]); $rows = []; foreach ($paginator->items() as $c) { $rows[] = [ 'id' => $c->id, 'nickname' => $c->user->nickname ?? '游客', 'article' => $c->article->title ?? '-', 'content' => $c->content, 'status' => $c->status, 'create_at' => $c->create_at, ]; } return json(['code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $rows]); } return View::fetch('comment/index', ['status' => $status, 'keyword' => $keyword]); } public function audit($id = 0) { $id = $id ?: (int) Request::post('id', 0); $comment = ArticleComment::find($id); if ($comment) { // 只在「未通过 → 通过」时累加计数:重复审核不会把评论数刷高 $wasPending = (int) $comment->status !== 1; $comment->status = 1; $comment->save(); if ($wasPending) { Article::where('id', $comment->aid)->inc('comment_count')->update(); } return json(['code' => 1, 'msg' => '审核通过']); } return json(['code' => 0, 'msg' => '评论不存在']); } public function delete($id = 0) { $id = $id ?: (int) Request::post('id', 0); $comment = ArticleComment::find($id); if ($comment) { $comment->delete(); // 同步回退文章评论数,保持计数准确 Article::where('id', $comment->aid)->where('comment_count', '>', 0)->dec('comment_count')->update(); return json(['code' => 1, 'msg' => '已删除']); } return json(['code' => 0, 'msg' => '评论不存在']); } }