42 lines
1.4 KiB
PHP
42 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ShopController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{ #untuk menampilkan halaman toko, termasuk menampilkan daftar produk yang tersedia dan memungkinkan pengguna untuk mencari dan menyaring produk
|
|
$query = Product::query()->where('stock', '>', 0);
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where(function ($q) use ($request) {
|
|
$q->where('name', 'like', '%' . $request->search . '%')
|
|
->orWhere('description', 'like', '%' . $request->search . '%');
|
|
});
|
|
}
|
|
|
|
if ($request->filled('age_range')) {
|
|
$query->where('age_range', 'like', '%' . $request->age_range . '%');
|
|
}
|
|
|
|
if ($request->filled('category')) {
|
|
$query->where('category', $request->category);
|
|
}
|
|
|
|
if ($request->filled('min_price')) {
|
|
$query->where('price', '>=', $request->min_price);
|
|
}
|
|
if ($request->filled('max_price')) {
|
|
$query->where('price', '<=', $request->max_price);
|
|
}
|
|
|
|
$products = $query->latest()->paginate(12)->withQueryString();
|
|
$categories = Product::whereNotNull('category')->distinct()->pluck('category');
|
|
|
|
return view('user.shop', compact('products', 'categories'));
|
|
}
|
|
}
|