comments()->paginate($request->get('per_page', 20)); foreach ($comments as $comment) { $comment->user; $comment->reply; } return new CommentCollection($comments); } /** * 评论详情 * @param Article $article * @param Comment $comment * @return CommentResource */ public function show(Article $article, Comment $comment) { $comment->user; $comment->reply; return new CommentResource($comment); } public function store(Request $request, Article $article, Comment $comment) { $this->validate($request, [ 'content' => 'required|max:2', ]); $comment->article()->associate($article); $comment->uid = Auth::auth(); $comment->save(); return new CommentResource($comment); } /** * 删除评论 * @param Article $article * @param Comment $comment * @return CommentResource * @throws \Illuminate\Auth\Access\AuthorizationException */ public function destroy(Article $article, Comment $comment) { $this->authorize('delete', $comment); $comment->delete(); return new CommentResource($comment); } }