has('post_type')) { $postType = $request->input('post_type'); if (in_array($postType, ['news', 'event'])) { $query->ofType($postType); } } // Filter by category (slug or id) if ($request->has('category')) { $category = $request->input('category'); $query->byCategory($category); } // Filter by status (default to published for frontend) $status = $request->input('status', 'publish'); if ($status === 'publish') { $query->published(); } elseif ($status === 'draft') { $query->draft(); } // Load categories relationship $query->with('categories'); // Order by published date $query->ordered(); // Pagination $perPage = $request->input('per_page', 15); $posts = $query->paginate($perPage); return response()->json($posts); } /** * Display the specified post. * * @param int $id * @return JsonResponse */ public function show(int $id): JsonResponse { $post = Post::with('categories')->findOrFail($id); return response()->json([ 'data' => $post, ]); } /** * Display the specified post by slug. * * @param string $slug * @return JsonResponse */ public function showBySlug(string $slug): JsonResponse { $post = Post::with('categories')->where('slug', $slug)->firstOrFail(); return response()->json([ 'data' => $post, ]); } /** * Store a newly created post in storage. * * @param Request $request * @return JsonResponse */ public function store(Request $request): JsonResponse { $validated = $request->validate([ 'title' => 'required|string|max:500', 'slug' => 'required|string|max:500|unique:posts,slug', 'content' => 'nullable|string', 'excerpt' => 'nullable|string', 'status' => ['nullable', Rule::in(['draft', 'publish'])], 'post_type' => ['required', Rule::in(['news', 'event'])], 'published_at' => 'nullable|date', 'date' => 'nullable|date', 'time' => 'nullable|string|max:50', 'image_url' => 'nullable|string|max:500', 'address' => 'nullable|string|max:500', 'description' => 'nullable|string', 'link' => 'nullable|string|max:500', 'featured_image' => 'nullable|boolean', 'categories' => 'nullable|array', 'categories.*' => 'exists:categories,id', ]); // Extract categories from validated data $categories = $validated['categories'] ?? []; unset($validated['categories']); // Create post $post = Post::create($validated); // Attach categories if provided if (!empty($categories)) { $post->categories()->attach($categories); } // Load categories relationship $post->load('categories'); return response()->json([ 'message' => 'Post created successfully', 'data' => $post, ], 201); } /** * Update the specified post in storage. * * @param Request $request * @param int $id * @return JsonResponse */ public function update(Request $request, int $id): JsonResponse { $post = Post::findOrFail($id); $validated = $request->validate([ 'title' => 'sometimes|required|string|max:500', 'slug' => 'sometimes|required|string|max:500|unique:posts,slug,' . $id, 'content' => 'nullable|string', 'excerpt' => 'nullable|string', 'status' => ['nullable', Rule::in(['draft', 'publish'])], 'post_type' => ['sometimes', 'required', Rule::in(['news', 'event'])], 'published_at' => 'nullable|date', 'date' => 'nullable|date', 'time' => 'nullable|string|max:50', 'image_url' => 'nullable|string|max:500', 'address' => 'nullable|string|max:500', 'description' => 'nullable|string', 'link' => 'nullable|string|max:500', 'featured_image' => 'nullable|boolean', 'categories' => 'nullable|array', 'categories.*' => 'exists:categories,id', ]); // Extract categories from validated data $categories = $validated['categories'] ?? null; unset($validated['categories']); // Update post $post->update($validated); // Sync categories if provided if ($categories !== null) { $post->categories()->sync($categories); } // Load categories relationship $post->load('categories'); return response()->json([ 'message' => 'Post updated successfully', 'data' => $post, ]); } /** * Remove the specified post from storage. * * @param int $id * @return JsonResponse */ public function destroy(int $id): JsonResponse { $post = Post::findOrFail($id); $post->delete(); return response()->json([ 'message' => 'Post deleted successfully', ]); } }