latest()->paginate(30); // 返回分类列表视图,并传递分类数据 return view('categories.index', compact('categories')); } /** * 显示分类详情 * * @param Request $request 请求对象 * @param Category $category 分类模型实例 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 返回分类详情视图 */ public function show(Request $request, Category $category) { // 获取排序字段,默认为 'created_at' $orderBy = $request->input('orderBy', 'created_at'); // 获取该分类下的产品,并按指定字段排序,分页显示,每页 10 条 $categoryProducts = $category->products() ->withCount('users') // 计算每个产品的用户数量 ->orderBy($orderBy, 'desc') ->paginate(10); // 返回分类详情视图,并传递分类及其产品数据 return view('categories.show', compact('category', 'categoryProducts')); } }