search and filtering

This commit is contained in:
Doar Mihai
2026-02-07 14:36:33 +02:00
parent 7a99c3eb59
commit 89be4d4329
10 changed files with 1506 additions and 23 deletions
+19 -7
View File
@@ -16,15 +16,27 @@ class ProductController extends Controller
*/
public function index(Request $request)
{
$query = Product::with(['user', 'category'])
->approved()
->latest();
$validated = $request->validate([
'search' => 'nullable|string|max:255',
'category' => 'nullable|integer|exists:categories,id',
'status' => 'nullable|string|in:pending,approved,rejected',
'date_range' => 'nullable|string|in:today,week,month,year',
'sort' => 'nullable|string|in:newest,oldest,popular',
]);
if ($request->has('category')) {
$query->where('category_id', $request->category);
}
// Build the query with all filters and search
$query = Product::query()
->with(['user', 'category'])
->approved() // Only show approved products on the public index
->search($request->search)
->byCategory($request->category)
->dateRange($request->date_range)
->sortBy($request->sort);
$products = $query->paginate(12);
// Paginate results with query parameters preserved
$products = $query->paginate(12)->appends($request->query());
// Get all categories for the filter dropdown
$categories = Category::all();
return view('products.index', compact('products', 'categories'));