latest()->paginate(15); return new PostCollection($posts); } /** * Store a newly created post. */ public function store(StorePostRequest $request): JsonResponse { $post = Post::create([ 'title' => $request->title, 'content' => $request->content, 'user_id' => $request->user()->id, ]); $post->load('user'); return response()->json([ 'message' => 'Post created successfully', 'post' => new PostResource($post), ], 201); } /** * Display the specified post. */ public function show(Post $post): JsonResponse { $post->load('user'); return response()->json([ 'post' => new PostResource($post), ]); } /** * Update the specified post. */ public function update(UpdatePostRequest $request, Post $post): JsonResponse { $this->authorize('update', $post); $post->update($request->validated()); return response()->json([ 'message' => 'Post updated successfully', 'post' => new PostResource($post), ]); } /** * Remove the specified post. */ public function destroy(Post $post): JsonResponse { $this->authorize('delete', $post); $post->delete(); return response()->json([ 'message' => 'Post deleted successfully', ]); } }