search and filtering
This commit is contained in:
@@ -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'));
|
||||
|
||||
Reference in New Issue
Block a user