43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$categoryId = $request->query('category');
|
|
$q = trim((string) $request->query('q', ''));
|
|
|
|
$categories = Category::ofType('product')->orderBy('sort')->orderBy('name')->get();
|
|
|
|
$products = Product::published()
|
|
->when($categoryId, fn ($query) => $query->where('category_id', $categoryId))
|
|
->when($q, function ($query) use ($q) {
|
|
$query->where(function ($sub) use ($q) {
|
|
$sub->where('name', 'like', "%{$q}%")
|
|
->orWhere('model', 'like', "%{$q}%")
|
|
->orWhere('summary', 'like', "%{$q}%");
|
|
});
|
|
})
|
|
->paginate(12)
|
|
->withQueryString();
|
|
|
|
return view('products.index', compact('products', 'categories', 'q', 'categoryId'));
|
|
}
|
|
|
|
public function show(Product $product): View
|
|
{
|
|
if ($product->status !== 'published') {
|
|
abort(404);
|
|
}
|
|
|
|
return view('products.show', compact('product'));
|
|
}
|
|
}
|