Files
ai-nav/web10/app/Http/Controllers/CategoryController.php

56 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2026-02-05 22:22:10 +08:00
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Tag;
use App\Models\SiteSetting;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::withCount('products')
->whereNull('parent_id')
->orderBy('sort')
->get();
2026-02-07 22:55:07 +08:00
return view('frontend.category.index', [
2026-02-05 22:22:10 +08:00
'categories' => $categories,
]);
}
public function show(string $slug)
{
$category = Category::where('slug', $slug)->firstOrFail();
$perPage = (int) SiteSetting::value('list_page_size', 20);
$moreThreshold = (int) SiteSetting::value('list_more_threshold', 20);
$tagSlug = request()->query('tag');
$pricing = request()->query('pricing');
$tags = Tag::orderBy('name')->get();
$products = $category->products()
->published()
->when($tagSlug, function ($builder) use ($tagSlug) {
$builder->whereHas('tags', function ($query) use ($tagSlug) {
$query->where('slug', $tagSlug);
});
})
->when($pricing, function ($builder) use ($pricing) {
$builder->where('pricing_type', $pricing);
})
->orderBy('sort')
->orderByDesc('hot_score')
->paginate($perPage);
2026-02-07 22:55:07 +08:00
return view('frontend.category.show', [
2026-02-05 22:22:10 +08:00
'category' => $category,
'products' => $products,
'moreThreshold' => $moreThreshold,
'tags' => $tags,
'tagSlug' => $tagSlug,
'pricing' => $pricing,
]);
}
}