Sistema de Ventas uses Laravel controllers to handle HTTP requests and implement business logic. Controllers are located in app/Http/Controllers/ and follow the single responsibility principle.
Displays paginated list of products with category information.
ProductoController.php:13-25
public function index(){ $categoria = DB::select("select * from categoria"); $datos = DB::table("producto") ->join("categoria", "producto.id_categoria", "=", "categoria.id_categoria") ->select("producto.*", "categoria.nombre as categoria") ->paginate(10); return view("vistas/productos/indexProducto", compact("datos")) ->with("categoria", $categoria);}
Returns: View with paginated products and categories
create() - Show Creation Form
Displays the form to create a new product.
ProductoController.php:30-34
public function create(){ $categoria = DB::select("select * from categoria"); return view("vistas/productos/registroProductos")->with("categoria", $categoria);}
Returns: Product creation form with available categories
store() - Create Product
Creates a new product with validation and optional image upload.Validation Rules:
txtcategoria: required
txtcodigoproducto: required, unique
txtnombreproducto: required
txtprecioproducto: required, numeric
txtstock: required, numeric
txtfoto: optional, mimes:png,jpg,jpeg
ProductoController.php:39-96
public function store(Request $request){ $request->validate([ "txtcategoria" => "required", "txtcodigoproducto" => "required", "txtnombreproducto" => "required", "txtprecioproducto" => "required|numeric", "txtstock" => "required|numeric", "txtfoto" => "mimes:png,jpg,jpeg" ]); // Check for duplicate product code $producto = DB::select("select count(*) as total from producto where codigo=?", [$request->txtcodigoproducto]); if ($producto[0]->total > 0) { return back()->with("INCORRECTO", "El producto ya se encuentra registrado"); } // Insert product and get ID $registro = DB::table("producto")->insertGetId([ "id_categoria" => $request->txtcategoria, "codigo" => $request->txtcodigoproducto, "nombre" => $request->txtnombreproducto, "precio" => $request->txtprecioproducto, "stock" => $request->txtstock, "descripcion" => $request->txtdescripcion, "estado" => "1" ]); // Handle file upload try { $foto = $request->file("txtfoto"); $nombreFoto = $registro . "-" . $foto->getClientOriginalName(); $ruta = storage_path("app/public/FOTO-PRODUCTOS/" . $nombreFoto); copy($foto, $ruta); } catch (\\Throwable $th) { $nombreFoto = ""; } // Update product with photo filename DB::update("update producto set foto=? where id_producto=?", [ $nombreFoto, $registro ]); return back()->with("CORRECTO", "Producto registrado correctamente");}
Returns: Redirect with success/error message
show() - Display Product
Shows detailed information about a specific product.
ProductoController.php:102-119
public function show(string $id){ $datos = DB::select(" SELECT producto.*, categoria.nombre as categoria FROM producto INNER JOIN categoria ON producto.id_categoria = categoria.id_categoria where id_producto=? ", [$id]); $categoria = DB::select("select * from categoria"); if (count($datos) <= 0) { return back()->with("INCORRECTO", "El producto no existe"); } return view("vistas/productos/showProducto", compact("datos", "categoria"));}
Parameters: $id - Product ID Returns: Product detail view or error redirect
update() - Update Product
Updates an existing product with validation.Validation Rules:
Same as store(), but allows same code if it’s the current product
ProductoController.php:132-169
public function update(Request $request, string $id){ $request->validate([ "txtcategoria" => "required", "txtcodigoproducto" => "required", "txtnombreproducto" => "required", "txtprecioproducto" => "required|numeric", "txtstock" => "required|numeric", ]); // Check for duplicate code (excluding current product) $duplicidadCodigo = DB::select( " select count(*) as total from producto where codigo=? and id_producto<>? ", [$request->txtcodigoproducto, $id] ); if ($duplicidadCodigo[0]->total > 0) { return back()->with("INCORRECTO", "El codigo ya se encuentra registrado"); } $actualizar = DB::update( " update producto set id_categoria=?, codigo=?, nombre=?, precio=?, stock=?, descripcion=? where id_producto=? ", [ $request->txtcategoria, $request->txtcodigoproducto, $request->txtnombreproducto, $request->txtprecioproducto, $request->txtstock, $request->txtdescripcion, $id ] ); return back()->with("CORRECTO", "Producto actualizado correctamente");}
Parameters: $id - Product ID Returns: Redirect with success/error message
destroy() - Delete Product
Deletes a product by code.
ProductoController.php:174-194
public function destroy(string $id){ $verificar = DB::select(" select count(*) as total from producto where codigo=?", [$id]); if ($verificar[0]->total <= 0) { return back()->with("INCORRECTO", "El producto no existe"); } try { $eliminar = DB::delete(" delete from producto where codigo=?", [$id]); } catch (\\Throwable $th) { $eliminar = false; } if ($eliminar == true) { return redirect()->route("productos.index")->with("CORRECTO", "Producto eliminado correctamente"); } else { return redirect()->route("productos.index")->with("INCORRECTO", "Error al eliminar el producto"); }}
Parameters: $id - Product code (not ID) Returns: Redirect to products list with message
buscarProducto() - Search Products (AJAX)
Searches products by code or name for autocomplete features.
ProductoController.php:196-218
public function buscarProducto(Request $request){ $id = $request->buscar; if ($id == null) { return response()->json([ "success" => false, "dato" => [] ], 400); } $datos = DB::select(" SELECT producto.*, categoria.nombre as cate FROM producto INNER JOIN categoria ON producto.id_categoria = categoria.id_categoria where codigo like '%$id%' or producto.nombre like '%$id%' limit 5 "); return response()->json([ "success" => true, "dato" => $datos ], 200);}
Request: buscar - Search term Returns: JSON with matching products (max 5)
public function index(){ $categorias = DB::select(" select * from categoria "); return view("vistas/categoria/indexCategoria", compact("categorias"));}
Returns: View with all categories
store() - Create Category
Creates a new category with duplicate validation.
CategoriaController.php:30-56
public function store(Request $request){ $request->validate([ "txtnombrecategoria" => "required", ]); // Validate unique name $existeCategoria = DB::select( " select count(*) as total from categoria where nombre=? ", [$request->txtnombrecategoria] ); if ($existeCategoria[0]->total >= 1) { return redirect()->back()->with("INCORRECTO", "El nombre de la categoria ya existe"); } try { $res = DB::insert(" insert into categoria(nombre) values(?) ", [ $request->txtnombrecategoria ]); } catch (\\Throwable $th) { $res = 0; } return redirect()->route("categoria.index")->with("CORRECTO", "Categoria registrada correctamente");}
Validation: Category name must be unique Returns: Redirect with message
update() - Update Category
Updates category with duplicate checking.
CategoriaController.php:79-111
public function update(Request $request, string $id){ $request->validate([ "txtnombrecategoria" => "required", ]); // Check if name exists for other categories $existeCategoria = DB::select( " select count(*) as total from categoria where nombre=? and id_categoria!=? ", [ $request->txtnombrecategoria, $id ] ); if($existeCategoria[0]->total >= 1){ return redirect()->back()->with("INCORRECTO", "El nombre de la categoria ya existe"); } $res = DB::update(" update categoria set nombre=? where id_categoria=? ", [ $request->txtnombrecategoria, $id ]); return redirect()->route("categoria.index")->with("CORRECTO", "Categoria actualizada correctamente");}
Parameters: $id - Category ID Returns: Redirect with message
destroy() - Delete Category
CategoriaController.php:116-130
public function destroy(string $id){ try { $res = DB::delete(" delete from categoria where id_categoria=? ", [$id]); } catch (\\Throwable $th) { $res = 0; } return redirect()->route("categoria.index")->with("CORRECTO", "Categoria eliminada correctamente");}
Parameters: $id - Category ID Returns: Redirect with message Note: May fail if products reference this category (foreign key constraint)
$existe = DB::select("select count(*) as total from table where field=?", [$value]);if ($existe[0]->total > 0) { return back()->with("INCORRECTO", "Already exists");}