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
Extends
Illuminate \ Database \ Eloquent \ Model
Traits
HasFactory - Enables model factory support for testing and seeding
Fillable Attributes
The following attributes are mass assignable:
Customer’s email address for communication
Customer status (e.g., active, inactive)
Customer segmentation classification:
premium - High-value customers
regular - Standard customers
ocasional - Occasional customers
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 );
}
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 );
}
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 );
}
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
Show View Complete Cliente Model Source Code
<? php
namespace App\Models ;
use Illuminate\Database\Eloquent\Factories\ HasFactory ;
use Illuminate\Database\Eloquent\ Model ;
class Cliente extends Model
{
use HasFactory ;
protected $fillable = [
'nombre' ,
'apellido' ,
'email' ,
'telefono' ,
'estado' ,
'segmento' ,
'total_compras' ,
];
public function ventas ()
{
return $this -> hasMany ( Venta :: class );
}
public function facturas ()
{
return $this -> hasMany ( Factura :: class );
}
public function mensajes ()
{
return $this -> hasMany ( Mensaje :: class );
}
}
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 ();