Skip to main content

Endpoint

/database/subcategorias.php

Get All Subcategories

Retrieve all subcategories or filter by category.

Get All Subcategories

curl -X GET https://your-domain.com/database/subcategorias.php

Get Subcategories by Category

curl -X GET "https://your-domain.com/database/subcategorias.php?categoria_id=3"

Query Parameters

categoria_id
integer
Filter subcategories by parent category ID

Response

{
  "success": true,
  "data": [
    {
      "id": 5,
      "categoria_id": 3,
      "nombre": "Smartphones",
      "descripcion": "Teléfonos inteligentes",
      "color": "#2e6df6",
      "estado": "ACTIVO",
      "fecha_creacion": "2026-03-02 09:15:00",
      "fecha_actualizacion": null
    },
    {
      "id": 4,
      "categoria_id": 3,
      "nombre": "Laptops",
      "descripcion": "Computadoras portátiles",
      "color": "#1abc9c",
      "estado": "ACTIVO",
      "fecha_creacion": "2026-03-01 16:20:00",
      "fecha_actualizacion": null
    }
  ]
}

Response Fields

id
integer
Unique subcategory identifier
categoria_id
integer
Parent category ID
nombre
string
Subcategory name (max 50 characters)
descripcion
string
Subcategory description (max 200 characters)
color
string
Hex color code for UI display
estado
string
Subcategory status
fecha_creacion
datetime
Creation timestamp
fecha_actualizacion
datetime
Last update timestamp (null if never updated)

Create Subcategory

Create a new subcategory within a category.
curl -X POST https://your-domain.com/database/subcategorias.php \
  -H "Content-Type: application/json" \
  -d '{
    "categoria_id": 3,
    "nombre": "Smartphones",
    "descripcion": "Teléfonos inteligentes",
    "color": "#2e6df6",
    "estado": "ACTIVO"
  }'

Request Parameters

categoria_id
integer
required
Parent category ID (must be greater than 0 and exist)
nombre
string
required
Subcategory name (max 50 characters, unique within category)
descripcion
string
Subcategory description (max 200 characters)
color
string
default:"#2e6df6"
Hex color code for UI display
estado
string
default:"ACTIVO"
Subcategory status

Validation Rules

  • categoria_id: Required, must be greater than 0, category must exist
  • nombre: Required, max 50 characters, unique within the same category
  • descripcion: Optional, max 200 characters
  • color: Optional, defaults to #2e6df6
  • estado: Optional, defaults to ACTIVO

Response

{
  "success": true,
  "id": 6,
  "message": "Subcategoría creada exitosamente"
}
Subcategory names must be unique within the same category. You can have the same subcategory name in different categories.

Update Subcategory

Update an existing subcategory’s information.
curl -X PUT https://your-domain.com/database/subcategorias.php \
  -H "Content-Type: application/json" \
  -d '{
    "id": 6,
    "nombre": "Smartphones Premium",
    "descripcion": "Teléfonos de alta gama",
    "color": "#9b59b6",
    "estado": "ACTIVO"
  }'

Request Parameters

id
integer
required
Subcategory ID to update (must be greater than 0)
nombre
string
required
New subcategory name (max 50 characters)
descripcion
string
New description (max 200 characters)
color
string
default:"#2e6df6"
New color code
estado
string
default:"ACTIVO"
New status

Validation Rules

  • id: Required, must be greater than 0, subcategory must exist
  • nombre: Required, max 50 characters, unique within category (excluding current)
  • descripcion: Optional, max 200 characters
  • Cannot change categoria_id (parent category)
  • Updates fecha_actualizacion timestamp automatically

Response

{
  "success": true,
  "message": "Subcategoría actualizada exitosamente"
}
You cannot change the parent category of a subcategory. If you need to move a subcategory to a different category, delete it and create a new one.

Delete Subcategory

Delete a subcategory from the system.
curl -X DELETE https://your-domain.com/database/subcategorias.php \
  -H "Content-Type: application/json" \
  -d '{"id": 6}'

Request Parameters

id
integer
required
Subcategory ID to delete (must be greater than 0)

Validation Rules

  • id: Required, must be greater than 0, subcategory must exist
  • Cannot delete if subcategory has associated products
  • Checks productos table for subcategoria_id foreign key

Response

{
  "success": true,
  "message": "Subcategoría eliminada exitosamente"
}
Deleting a subcategory is prevented if it has associated products. You must reassign or delete the products first.

Database Schema

Subcategories are stored in the subcategorias table:
CREATE TABLE subcategorias (
  id INT AUTO_INCREMENT PRIMARY KEY,
  categoria_id INT NOT NULL,
  nombre VARCHAR(50) NOT NULL,
  descripcion VARCHAR(200),
  color VARCHAR(7) DEFAULT '#2e6df6',
  estado VARCHAR(20) DEFAULT 'ACTIVO',
  fecha_creacion DATETIME,
  fecha_actualizacion DATETIME,
  FOREIGN KEY (categoria_id) REFERENCES categorias(id),
  UNIQUE KEY unique_nombre_per_categoria (categoria_id, nombre)
);

Error Codes

Error MessageCauseSolution
Datos inválidosInvalid JSONCheck JSON syntax
Categoría inválidaInvalid categoria_idProvide valid category ID
Categoría no encontradaCategory doesn’t existCheck category exists
El nombre es obligatorioMissing nombreInclude nombre in request
El nombre no puede superar 50 caracteresName too longShorten subcategory name
La descripción no puede superar 200 caracteresDescription too longShorten description
Ya existe una subcategoría con este nombre en esta categoríaDuplicate within categoryUse different name or different category
ID inválidoInvalid IDProvide valid ID > 0
Subcategoría no encontradaSubcategory doesn’t existCheck subcategory ID
No se puede eliminar. Tiene X producto(s) asociado(s)Has productsRemove products first

Build docs developers (and LLMs) love