This commit is contained in:
gianella
2026-04-04 15:14:28 -03:00
parent 778871809d
commit 956b2469f9
6 changed files with 316 additions and 61 deletions
+16 -6
View File
@@ -12,26 +12,36 @@ class CatalogoController extends Controller
*/
public function index(Request $request)
{
// Consulta base
// --- 1. CONSULTA PARA LA GRILLA (PAGINADA) ---
$query = Product::query();
// Lógica del Buscador: Si recibimos algo en el input "search"
// Lógica del Buscador tradicional (por si seguís usando un input de texto además de Select2)
if ($request->has('search')) {
$searchTerm = $request->input('search');
$query->where(function($q) use ($searchTerm) {
$q->where('name', 'like', "%{$searchTerm}%")
->orWhere('sku', 'like', "%{$searchTerm}%");
->orWhere('sku', 'like', "%{$searchTerm}%");
});
}
// Filtros base para la grilla
$query->whereIn('type', ['bike', 'accessory', 'clothing', 'spare']);
$query->where('stock_quantity', '>', 0); // Solo mostrar si tiene stock
// Resultados paginados
$products = $query->paginate(12)->withQueryString(); // withQueryString mantiene la búsqueda al cambiar de página
$products = $query->paginate(12)->withQueryString();
// Devuelve la vista
return view('catalogo.index', compact('products'));
// --- 2. CONSULTA PARA EL BUSCADOR SELECT2 (TODOS) ---
// Traemos todos los productos válidos para llenar el desplegable de búsqueda rápida.
// Hacemos la misma validación de stock y tipo para no mostrar cosas agotadas.
$allProducts = Product::whereIn('type', ['bike', 'accessory', 'clothing', 'spare'])
->where('stock_quantity', '>', 0)
->orderBy('name') // Ordenados alfabéticamente
->get();
// Devuelve la vista pasando ambas variables
return view('catalogo.index', compact('products', 'allProducts'));
}
public function show(Product $product)