php artisan migrate:fresh y php artisan importar:excel, les tengo que pasar el archivo csv
This commit is contained in:
@@ -8,39 +8,39 @@ use Illuminate\Http\Request;
|
||||
class CatalogoController extends Controller
|
||||
{
|
||||
/**
|
||||
* Muestra los registros de ventas con buscador y paginación.
|
||||
* Muestra el catálogo público de productos.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
// --- 1. CONSULTA PARA LA GRILLA (PAGINADA) ---
|
||||
$query = Product::query();
|
||||
// 1. Atrapamos lo que el usuario escribió o seleccionó
|
||||
$query = $request->input('search');
|
||||
$type = $request->input('type');
|
||||
|
||||
// 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');
|
||||
// --- CONSULTA PARA LA GRILLA (PAGINADA) ---
|
||||
$productsQuery = Product::query();
|
||||
|
||||
$query->where(function($q) use ($searchTerm) {
|
||||
$q->where('name', 'like', "%{$searchTerm}%")
|
||||
->orWhere('sku', 'like', "%{$searchTerm}%");
|
||||
});
|
||||
$productsQuery->when($query, function ($q) use ($query) {
|
||||
return $q->where('name', 'like', "%{$query}%");
|
||||
});
|
||||
|
||||
if ($type) {
|
||||
$productsQuery->where('type', $type);
|
||||
} else {
|
||||
$productsQuery->where('type', '!=', 'service');
|
||||
}
|
||||
|
||||
// Filtros base para la grilla
|
||||
$query->whereIn('type', ['bike', 'accessory', 'clothing', 'spare']);
|
||||
$query->where('stock_quantity', '>', 0); // Solo mostrar si tiene stock
|
||||
//mostrar si tiene stock mayor a 0
|
||||
$productsQuery->where('stock_quantity', '>', 0);
|
||||
|
||||
// Resultados paginados
|
||||
$products = $query->paginate(12)->withQueryString();
|
||||
$products = $productsQuery->orderBy('name', 'asc')->paginate(12)->withQueryString();
|
||||
|
||||
// --- 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'])
|
||||
$allProducts = Product::where('type', '!=', 'service')
|
||||
->where('stock_quantity', '>', 0)
|
||||
->orderBy('name') // Ordenados alfabéticamente
|
||||
->orderBy('name', 'asc')
|
||||
->get();
|
||||
|
||||
// Devuelve la vista pasando ambas variables
|
||||
// Devuelve la vista
|
||||
return view('catalogo.index', compact('products', 'allProducts'));
|
||||
}
|
||||
|
||||
@@ -48,4 +48,4 @@ class CatalogoController extends Controller
|
||||
{
|
||||
return view('catalogo.show', compact('product'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user