1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Services\LiquidRenderer;
- use App\Models\DistProduct;
- use App\Models\DistProductCategory; // 引入分类模型
- class ProductController extends Controller
- {
- protected $liquidRenderer;
- public function __construct(LiquidRenderer $liquidRenderer)
- {
- $this->liquidRenderer = $liquidRenderer;
- }
- /**
- * Display a listing of the products.
- *
- * @return \Illuminate\Http\Response
- */
- // public function index()
- // {
- // $products = Product::paginate(10); // 每页显示10个产品
- // return $this->liquidRenderer->render('products.index', ['products' => $products]);
- // }
- public function category($slug)
- {
- // $products = DistProduct::paginate(10); // 每页显示10个产品
- // return $this->liquidRenderer->render('products_categories.liquid', ['products' => $products]);
- // 获取分类信息
- $category = DistProductCategory::findOrFail($slug);
- // 获取分类下的所有产品,并按 is_pinned 排序,然后进行分页
- $products = DistProduct::where('category_id', $category->id)
- ->where('dist_id', getDistId())
- ->where('enabled', 1)
- ->with('images') // Eager load images
- ->orderBy('is_pinned', 'desc') // 按 is_pinned 降序排序
- ->paginate(12);
- // 渲染模板并传递数据
- return $this->liquidRenderer->render('products_categories.liquid', [
- 'category' => $category, // 分类名称
- 'products' => $products, // 分类下的产品
- ]);
- }
- /**
- * Display the specified product.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function detail($id)
- {
- $product = DistProduct::getProduct($id);
- return $this->liquidRenderer->render('products_detail.liquid',
- [
- 'product' => $product,
- 'csrf_token' => csrf_token()
- ]);
- }
- }
|