Skip to main content

Overview

The Factura model represents invoices in the Dashboard Laravel application. Each invoice is linked to a customer and optionally to a sale (venta). The model includes payment status tracking, emission and due dates, and automatic date casting.
The Factura model provides complete invoice management with date casting for proper date handling and relationships to both customers and sales.

Model Details

Namespace

App\Models\Factura

Extends

Illuminate\Database\Eloquent\Model

Traits

  • HasFactory - Enables model factory support for testing and seeding

Fillable Attributes

The following attributes are mass assignable:
numero_factura
string
required
Unique invoice number identifier
cliente_id
integer
required
Foreign key reference to the Cliente model
venta_id
integer
Foreign key reference to the Venta model (optional)
concepto
string
required
Invoice concept or description of billed items/services
monto
decimal
required
Total invoice amount
fecha_emision
date
required
Invoice emission/issue date (automatically cast to Carbon date)
fecha_vencimiento
date
required
Invoice due date for payment (automatically cast to Carbon date)
estado
string
required
Payment status of the invoice:
  • pendiente - Payment pending
  • pagada - Invoice paid
  • vencida - Invoice overdue
  • cancelada - Invoice cancelled

Attribute Casting

The model automatically casts date attributes to Carbon instances:
fecha_emision
date
Cast to Carbon date instance for easy date manipulation
fecha_vencimiento
date
Cast to Carbon date instance for due date calculations

Relationships

cliente() - Belongs To

Each invoice belongs to a single customer.
public function cliente()
{
    return $this->belongsTo(Cliente::class);
}
cliente
Cliente
The customer (Cliente) associated with this invoice
Usage:
$factura = Factura::find(1);
$cliente = $factura->cliente; // Get the customer
$emailCliente = $factura->cliente->email;

venta() - Belongs To

Each invoice can optionally belong to a sale.
public function venta()
{
    return $this->belongsTo(Venta::class);
}
venta
Venta
The sale (Venta) associated with this invoice (nullable)
Usage:
$factura = Factura::find(1);
$venta = $factura->venta; // Get the related sale

if ($factura->venta) {
    $numeroOrden = $factura->venta->numero_orden;
}

Database Configuration

Table Name

'facturas' // Laravel automatically pluralizes the model name

Timestamps

true // Uses created_at and updated_at timestamps

Full Model Code

Usage Examples

Creating a New Invoice

use App\Models\Factura;
use Carbon\Carbon;

$factura = Factura::create([
    'numero_factura' => 'INV-2026-001',
    'cliente_id' => 1,
    'venta_id' => 5,
    'concepto' => 'Venta de laptop y accesorios',
    'monto' => 1499.99,
    'fecha_emision' => Carbon::today(),
    'fecha_vencimiento' => Carbon::today()->addDays(30),
    'estado' => 'pendiente',
]);

Working with Date Casting

$factura = Factura::find(1);

// Dates are automatically Carbon instances
echo $factura->fecha_emision->format('d/m/Y'); // 08/03/2026
echo $factura->fecha_vencimiento->diffForHumans(); // "in 30 days"

// Check if invoice is overdue
if ($factura->fecha_vencimiento->isPast() && $factura->estado === 'pendiente') {
    $factura->update(['estado' => 'vencida']);
}

Accessing Invoice Relationships

// Eager load relationships
$factura = Factura::with(['cliente', 'venta'])->find(1);

// Access customer information
echo $factura->cliente->nombre . ' ' . $factura->cliente->apellido;

// Access related sale if exists
if ($factura->venta) {
    echo "Order: " . $factura->venta->numero_orden;
    echo "Product: " . $factura->venta->producto;
}

Querying Invoices by Status

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

// Get overdue invoices
$vencidas = Factura::where('estado', 'pendiente')
    ->where('fecha_vencimiento', '<', Carbon::today())
    ->get();

// Get paid invoices for a customer
$facturasPagadas = Factura::where('cliente_id', 1)
    ->where('estado', 'pagada')
    ->orderBy('fecha_emision', 'desc')
    ->get();

// Calculate total pending amount
$totalPendiente = Factura::where('estado', 'pendiente')->sum('monto');

Updating Invoice Status

// Mark invoice as paid
$factura = Factura::find(1);
$factura->update(['estado' => 'pagada']);

// Automatically mark overdue invoices
Factura::where('estado', 'pendiente')
    ->where('fecha_vencimiento', '<', Carbon::today())
    ->update(['estado' => 'vencida']);

Build docs developers (and LLMs) love