input('search', ''); $type = $request->input('type', 'all'); // all, news, event $perPage = $request->input('per_page', 12); // 建立查詢 $query = Post::query() ->published() ->with('categories') ->ordered(); // 根據類型篩選 if ($type !== 'all') { $query->ofType($type); } // 搜尋功能 if (!empty($search)) { $query->where(function ($q) use ($search) { $q->where('title', 'like', "%{$search}%") ->orWhere('content', 'like', "%{$search}%") ->orWhere('excerpt', 'like', "%{$search}%"); }); } // 分頁 $posts = $query->paginate($perPage); return Inertia::render('news', [ 'posts' => PostResource::collection($posts->items())->resolve(), 'pagination' => [ 'total' => $posts->total(), 'per_page' => $posts->perPage(), 'current_page' => $posts->currentPage(), 'last_page' => $posts->lastPage(), 'from' => $posts->firstItem(), 'to' => $posts->lastItem(), ], 'filters' => [ 'search' => $search, 'type' => $type, ], ]); } /** * Display the specified post (news or event detail). * * @param Post $post * @return \Inertia\Response */ public function show(Post $post) { // Load categories relationship $post->load('categories'); // 根據 post_type 決定要渲染的頁面 $viewName = $post->isEvent() ? 'event-detail' : 'news-detail'; return Inertia::render($viewName, [ 'post' => PostResource::make($post)->resolve(), ]); } }