Skip to main content

Overview

The Mantenimiento model represents maintenance operations performed on assets. It tracks preventive, corrective, and predictive maintenance activities, including assigned personnel, schedules, costs, and validation status.

Model Information

table
string
default:"mantenimientos"
Database table name
namespace
string
App\Models\Mantenimiento

Fillable Fields

The following fields can be mass-assigned:
activo_id
integer
required
Foreign key reference to the activos table. The asset being maintained.
supervisor_id
integer
required
Foreign key reference to the users table. The supervisor overseeing this maintenance operation.
tecnico_principal_id
integer
required
Foreign key reference to the users table. The primary technician responsible for this maintenance.
tipo
string
default:"preventivo"
Type of maintenance operation. Uses string values from TipoMantenimiento enum.Possible values:
  • correctivo - Corrective maintenance (fixing issues)
  • predictivo - Predictive maintenance (based on monitoring)
  • preventivo - Preventive maintenance (scheduled)
reporte_id
integer
default:"null"
Foreign key reference to the reportes table. Links maintenance to an issue report.Important: This field is nullable because preventive maintenance does not require a report, while corrective maintenance typically originates from a report.
fecha_apertura
datetime
required
Date and time when the maintenance operation was opened/started.
fecha_cierre
datetime
required
Date and time when the maintenance operation was completed/closed.
estado
string
default:"pendiente"
Current status of the maintenance operation. Uses string values from EstadoMantenimiento enum.Possible values:
  • pendiente - Pending
  • en_proceso - In progress
  • completado - Completed
  • cancelado - Cancelled
descripcion
string
required
Detailed description of the maintenance work to be performed or that was performed.
validado
boolean
required
Whether the maintenance work has been validated/approved by a supervisor.
costo_total
decimal
required
Total cost of the maintenance operation, including parts and labor.

Casts

The model automatically casts the following attributes:
protected $casts = [
    'estado' => 'string',
    'tipo' => 'string',
    'fecha_apertura' => 'datetime',
    'fecha_cierre' => 'datetime',
    'validado' => 'boolean',
    'costo_total' => 'decimal:2',
];

Relationships

Belongs To

activo

Returns the asset that this maintenance operation is performed on.
$mantenimiento = Mantenimiento::find(1);
$activo = $mantenimiento->activo;

echo $activo->articulo->modelo;  // Access asset's article model
echo $activo->estado;             // Access asset state
Relationship Details:
  • Type: BelongsTo
  • Related Model: App\Models\Activo
  • Foreign Key: activo_id

supervisor

Returns the user who supervises this maintenance operation.
$mantenimiento = Mantenimiento::find(1);
$supervisor = $mantenimiento->supervisor;

echo $supervisor->name;
echo $supervisor->email;
Relationship Details:
  • Type: BelongsTo
  • Related Model: App\Models\User
  • Foreign Key: supervisor_id

tecnicoPrincipal

Returns the primary technician assigned to this maintenance operation.
$mantenimiento = Mantenimiento::find(1);
$tecnico = $mantenimiento->tecnicoPrincipal;

echo $tecnico->name;
echo $tecnico->telefono;
Relationship Details:
  • Type: BelongsTo
  • Related Model: App\Models\User
  • Foreign Key: tecnico_principal_id

reporte

Returns the report that originated this maintenance (if applicable).
$mantenimiento = Mantenimiento::find(1);

if ($mantenimiento->reporte) {
    echo $mantenimiento->reporte->descripcion;
    echo $mantenimiento->reporte->prioridad;
}
Relationship Details:
  • Type: BelongsTo
  • Related Model: App\Models\Reporte
  • Foreign Key: reporte_id
  • Note: This relationship may be null for preventive maintenance

Has Many

sesiones

Returns all work sessions associated with this maintenance operation.
$mantenimiento = Mantenimiento::find(1);
$sesiones = $mantenimiento->sesiones;

foreach ($sesiones as $sesion) {
    echo $sesion->fecha_inicio;
    echo $sesion->duracion;
    echo $sesion->tecnico->name;
}
Relationship Details:
  • Type: HasMany
  • Related Model: App\Models\SesionesMantenimiento
  • Foreign Key: mantenimiento_id

Usage Examples

Creating Preventive Maintenance

use App\Models\Mantenimiento;
use Carbon\Carbon;

$mantenimiento = Mantenimiento::create([
    'activo_id' => 1,
    'supervisor_id' => 2,
    'tecnico_principal_id' => 3,
    'tipo' => 'preventivo',
    'reporte_id' => null, // Preventive maintenance has no report
    'fecha_apertura' => Carbon::now(),
    'fecha_cierre' => Carbon::now()->addDays(3),
    'estado' => 'pendiente',
    'descripcion' => 'Mantenimiento preventivo mensual - revisión general',
    'validado' => false,
    'costo_total' => 150.00,
]);

Creating Corrective Maintenance from Report

use App\Models\Mantenimiento;
use Carbon\Carbon;

$mantenimiento = Mantenimiento::create([
    'activo_id' => 5,
    'supervisor_id' => 2,
    'tecnico_principal_id' => 4,
    'tipo' => 'correctivo',
    'reporte_id' => 10, // Linked to a report
    'fecha_apertura' => Carbon::now(),
    'fecha_cierre' => Carbon::now()->addHours(6),
    'estado' => 'en_proceso',
    'descripcion' => 'Reparación de falla reportada en sistema de refrigeración',
    'validado' => false,
    'costo_total' => 450.00,
]);

Updating Maintenance Status

$mantenimiento = Mantenimiento::find(1);
$mantenimiento->estado = 'completado';
$mantenimiento->fecha_cierre = now();
$mantenimiento->save();

Validating Maintenance Work

$mantenimiento = Mantenimiento::find(1);
$mantenimiento->validado = true;
$mantenimiento->save();

Querying Maintenance Records

// Get all pending maintenance
$pendientes = Mantenimiento::where('estado', 'pendiente')->get();

// Get all preventive maintenance for a specific asset
$preventivos = Mantenimiento::where('activo_id', 1)
    ->where('tipo', 'preventivo')
    ->orderBy('fecha_apertura', 'desc')
    ->get();

// Get maintenance with all related data
$mantenimiento = Mantenimiento::with([
    'activo.articulo',
    'supervisor',
    'tecnicoPrincipal',
    'sesiones.tecnico',
    'reporte'
])->find(1);

Calculating Total Maintenance Cost for Asset

$activo = Activo::find(1);
$costoTotal = $activo->mantenimientos()
    ->where('estado', 'completado')
    ->sum('costo_total');

echo "Costo total de mantenimiento: $" . number_format($costoTotal, 2);

Getting Maintenance by Technician

$tecnico = User::find(3);
$mantenimientos = $tecnico->mantenimientosTecnicoPrincipal;

foreach ($mantenimientos as $mant) {
    echo "{$mant->activo->articulo->modelo} - {$mant->estado}";
}

Database Schema

CREATE TABLE mantenimientos (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    activo_id BIGINT UNSIGNED NOT NULL,
    supervisor_id BIGINT UNSIGNED NOT NULL,
    tecnico_principal_id BIGINT UNSIGNED NOT NULL,
    tipo VARCHAR(255) DEFAULT 'preventivo',
    reporte_id BIGINT UNSIGNED NULL,
    fecha_apertura DATETIME NOT NULL,
    fecha_cierre DATETIME NOT NULL,
    estado VARCHAR(255) DEFAULT 'pendiente',
    descripcion VARCHAR(255) NOT NULL,
    validado BOOLEAN NOT NULL,
    costo_total FLOAT NOT NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    
    FOREIGN KEY (activo_id) REFERENCES activos(id),
    FOREIGN KEY (supervisor_id) REFERENCES users(id),
    FOREIGN KEY (tecnico_principal_id) REFERENCES users(id),
    FOREIGN KEY (reporte_id) REFERENCES reportes(id) ON DELETE CASCADE
);
  • Activo - Assets being maintained
  • Reporte - Reports that trigger maintenance
  • User - Supervisors and technicians

Build docs developers (and LLMs) love