comments() ->with('author:id,name') ->get() ->map(fn($comment) => [ 'id' => $comment->id, 'content' => $comment->content, 'author' => $comment->author, 'created_at' => $comment->created_at, ]); return response()->json([ 'comments' => $comments ]); } public function store(Request $request, Post $post): JsonResponse { $validated = $request->validate([ 'content' => 'required|string', ]); $comment = $post->comments()->create([ ...$validated, 'author_id' => $request->user()->id, ]); $comment->load('author:id,name'); return response()->json([ 'message' => 'Comment added successfully', 'comment' => [ 'id' => $comment->id, 'content' => $comment->content, 'author' => $comment->author, 'created_at' => $comment->created_at, ] ]); } public function destroy(Comment $comment): JsonResponse { $this->authorize('delete', $comment); $comment->delete(); return response()->json([ 'message' => 'Comment deleted successfully' ]); } }