Skip to main content

Overview

The Cliente model represents customers in the Dashboard Laravel application. It includes customer segmentation capabilities (premium, regular, occasional) and maintains relationships with sales (ventas), invoices (facturas), and messages (mensajes).
The Cliente model tracks customer status, segment classification, and total purchase amounts for business intelligence and CRM purposes.

Model Details

Namespace

App\Models\Cliente

Extends

Illuminate\Database\Eloquent\Model

Traits

  • HasFactory - Enables model factory support for testing and seeding

Fillable Attributes

The following attributes are mass assignable:
nombre
string
required
Customer’s first name
apellido
string
required
Customer’s last name
email
string
required
Customer’s email address for communication
telefono
string
Customer’s phone number
estado
string
Customer status (e.g., active, inactive)
segmento
string
Customer segmentation classification:
  • premium - High-value customers
  • regular - Standard customers
  • ocasional - Occasional customers
total_compras
decimal
Total amount of all customer purchases

Relationships

ventas() - Has Many

A customer can have multiple sales records.
public function ventas()
{
    return $this->hasMany(Venta::class);
}
ventas
Venta[]
Collection of all sales (Venta) associated with this customer
Usage:
$cliente = Cliente::find(1);
$ventas = $cliente->ventas; // Get all sales
$ventasCount = $cliente->ventas()->count(); // Count sales

facturas() - Has Many

A customer can have multiple invoices.
public function facturas()
{
    return $this->hasMany(Factura::class);
}
facturas
Factura[]
Collection of all invoices (Factura) associated with this customer
Usage:
$cliente = Cliente::find(1);
$facturas = $cliente->facturas; // Get all invoices
$facturasPendientes = $cliente->facturas()->where('estado', 'pendiente')->get();

mensajes() - Has Many

A customer can have multiple messages.
public function mensajes()
{
    return $this->hasMany(Mensaje::class);
}
mensajes
Mensaje[]
Collection of all messages (Mensaje) associated with this customer
Usage:
$cliente = Cliente::find(1);
$mensajes = $cliente->mensajes; // Get all messages
$mensajesNoLeidos = $cliente->mensajes()->where('leido', false)->get();

Database Configuration

Table Name

'clientes' // Laravel automatically pluralizes the model name

Timestamps

true // Uses created_at and updated_at timestamps

Full Model Code

Usage Examples

Creating a New Customer

use App\Models\Cliente;

$cliente = Cliente::create([
    'nombre' => 'Juan',
    'apellido' => 'Pérez',
    'email' => '[email protected]',
    'telefono' => '+34 123 456 789',
    'estado' => 'activo',
    'segmento' => 'premium',
    'total_compras' => 1500.00,
]);

Accessing Customer Relationships

$cliente = Cliente::with(['ventas', 'facturas', 'mensajes'])->find(1);

// Get total sales amount
$totalVentas = $cliente->ventas->sum('total');

// Get pending invoices
$facturasPendientes = $cliente->facturas->where('estado', 'pendiente');

// Get unread messages count
$mensajesNoLeidos = $cliente->mensajes->where('leido', false)->count();

Customer Segmentation Query

// Get all premium customers
$premiumClientes = Cliente::where('segmento', 'premium')->get();

// Get customers with high purchase totals
$highValueClientes = Cliente::where('total_compras', '>', 5000)->get();

Build docs developers (and LLMs) love