56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?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();
|
|
|
|
return view('category.index', [
|
|
'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);
|
|
|
|
return view('category.show', [
|
|
'category' => $category,
|
|
'products' => $products,
|
|
'moreThreshold' => $moreThreshold,
|
|
'tags' => $tags,
|
|
'tagSlug' => $tagSlug,
|
|
'pricing' => $pricing,
|
|
]);
|
|
}
|
|
}
|