You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Category;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CategoryController extends Controller
|
|
{
|
|
/**
|
|
* 显示前台分类列表
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 返回分类列表视图
|
|
*/
|
|
public function index()
|
|
{
|
|
// 获取最新的分类,分页显示,每页 30 条
|
|
$categories = Category::query()->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'));
|
|
}
|
|
}
|