Skip to main content

Overview

The ClienteController manages client information in the Sistema de Abogados application. It provides complete CRUD (Create, Read, Update, Delete) operations with search functionality and displays relationships to expedientes. Namespace: App\Http\Controllers Extends: Controller

Dependencies

The controller uses the following models:
  • Cliente - Main client model
  • Expedientes - Expediente model for displaying client relationships

Methods

index()

Lists all clients with pagination and search functionality. Purpose: Display a paginated list of clients with optional search filtering Parameters: None (uses request('search') for search query) Returns: View clientes.index with clients Response Data:
clientes
Collection
Paginated collection of clients (5 per page)
Code Example:
public function index()
{
    $clientes = Cliente::search(request('search'))->paginate(5);
    return view('clientes.index', compact('clientes'));
}
Route: clientes.index Notes:
  • Implements search functionality through the Cliente model’s search scope
  • Paginates results at 5 clients per page

create()

Displays the client creation form. Purpose: Show the form for creating a new client Parameters: None Returns: View clientes.create Code Example:
public function create()
{
    return view('clientes.create');
}
Route: clientes.create

store()

Creates a new client with validation. Purpose: Store a new client in the database with comprehensive validation Request Parameters:
nombre
string
required
Client’s first name
apellidos
string
required
Client’s last name(s)
genero
string
Client’s gender (optional)
dni_ruc
string
required
Client’s DNI or RUC identification number
empresa
string
Company name if client represents an organization (optional)
email
string
Client’s email address (optional)
telefono
string
Client’s phone number (optional)
direccion
string
Client’s physical address (optional)
referencia
string
Address reference or additional notes (optional)
estado_cliente
string
required
Client status (e.g., Active, Inactive)
Validation Rules:
[
    'nombre' => 'required', 
    'apellidos' => 'required', 
    'genero' => 'nullable', 
    'dni_ruc' => 'required', 
    'empresa' => 'nullable',
    'email' => 'nullable', 
    'telefono' => 'nullable',
    'direccion' => 'nullable',
    'referencia' => 'nullable',
    'estado_cliente' => 'required'
]
Returns: Redirect to clientes.index with success message Success Message: “Cliente registrado correctamente.” Code Example:
public function store(Request $request)
{
    $validated = $request->validate([
        'nombre' => 'required', 
        'apellidos' => 'required', 
        'genero' => 'nullable', 
        'dni_ruc' => 'required', 
        'empresa' => 'nullable',
        'email' => 'nullable', 
        'telefono' => 'nullable',
        'direccion' => 'nullable',
        'referencia' => 'nullable',
        'estado_cliente' => 'required'
    ]);
    Cliente::create($validated);
    return to_route('clientes.index')
        ->with('message', 'Cliente registrado correctamente.');
}
Route: clientes.store

show()

Displays detailed information for a specific client. Purpose: Show complete client details including related expedientes Parameters:
cliente
Cliente
required
Client model instance (route model binding)
Returns: View clientes.cliente with client data and expedientes Response Data:
cliente
Cliente
The client instance with all details
expedientes
Collection
All expedientes in the system (for relationship display)
Code Example:
public function show(Cliente $cliente)
{
    $expedientes = Expedientes::all();
    return view('clientes.cliente', compact('cliente','expedientes'));
}
Route: clientes.show Notes:
  • Displays client information along with all expedientes
  • Useful for viewing client’s case history and relationships

edit()

Displays the client edit form. Purpose: Show the form for editing an existing client Parameters:
cliente
Cliente
required
Client model instance (route model binding)
Returns: View clientes.edit with client data Response Data:
cliente
Cliente
The client instance to edit
Code Example:
public function edit(Cliente $cliente)
{
    return view('clientes.edit', compact('cliente'));
}
Route: clientes.edit

update()

Updates an existing client with validation. Purpose: Update client information in the database Parameters:
cliente
Cliente
required
Client model instance (route model binding)
Request Parameters:
nombre
string
required
Client’s first name
apellidos
string
required
Client’s last name(s)
genero
string
Client’s gender (optional)
dni_ruc
string
required
Client’s DNI or RUC identification number
empresa
string
Company name (optional)
email
string
Client’s email address (optional)
telefono
string
Client’s phone number (optional)
direccion
string
Client’s physical address (optional)
referencia
string
Address reference (optional)
estado_cliente
string
required
Client status
Validation Rules:
[
    'nombre' => 'required', 
    'apellidos' => 'required',
    'genero' => 'nullable',
    'dni_ruc' => 'required',
    'empresa' => 'nullable',
    'email' => 'nullable',
    'telefono' => 'nullable',
    'direccion' => 'nullable',
    'referencia' => 'nullable',
    'estado_cliente'=> 'required'
]
Returns: Redirect to clientes.index with success message Success Message: “Permiso actualisado correctamente.” Code Example:
public function update(Request $request, Cliente $cliente)
{
    $validated = $request->validate([
        'nombre' => 'required', 
        'apellidos' => 'required',
        'genero' => 'nullable',
        'dni_ruc' => 'required',
        'empresa' => 'nullable',
        'email' => 'nullable',
        'telefono' => 'nullable',
        'direccion' => 'nullable',
        'referencia' => 'nullable',
        'estado_cliente'=> 'required'
    ]);
    $cliente->update($validated);

    return to_route('clientes.index')
        ->with('message', 'Permiso actualisado correctamente.');
}
Route: clientes.update Notes:
  • Success message says “Permiso actualisado” (Permission updated) - appears to be a typo in original code
  • Should likely say “Cliente actualisado correctamente”

destroy()

Deletes a client from the database. Purpose: Remove a client with foreign key constraint handling Parameters:
cliente
Cliente
required
Client model instance (route model binding)
Returns:
  • Success: Redirect to clientes.index with success message
  • Error: Back with alert message if constraints exist
Success Message: “Cliente eliminado correctamente.” Error Message: “Este cliente esta asignado a un expedientes.” Code Example:
public function destroy(Cliente $cliente)
{
    try{
        $cliente->delete();
        return to_route('clientes.index')
            ->with('messagedestroy', 'Cliente eliminado correctamente.');
    }catch(\Exception $e){
        return back()->with('messageAlert', 
            'Este cliente esta asignado a un expedientes.');
    }
}
Route: clientes.destroy Notes:
  • Uses try-catch to handle foreign key constraint violations
  • Client cannot be deleted if assigned to expedientes or casos
  • Must remove client associations before deletion

Usage Examples

Creating a New Client (Individual)

// POST /clientes
$data = [
    'nombre' => 'Juan Carlos',
    'apellidos' => 'Rodríguez Pérez',
    'genero' => 'M',
    'dni_ruc' => '12345678',
    'empresa' => null,
    'email' => '[email protected]',
    'telefono' => '+51 987654321',
    'direccion' => 'Av. Principal 123, Lima',
    'referencia' => 'Edificio azul, tercer piso',
    'estado_cliente' => 'Activo'
];

Creating a Corporate Client

// POST /clientes
$data = [
    'nombre' => 'María',
    'apellidos' => 'Gómez Torres',
    'genero' => 'F',
    'dni_ruc' => '20123456789', // RUC format
    'empresa' => 'Constructora ABC S.A.C.',
    'email' => '[email protected]',
    'telefono' => '+51 01 234 5678',
    'direccion' => 'Av. Industrial 456, Lima',
    'referencia' => 'Zona industrial, oficina principal',
    'estado_cliente' => 'Activo'
];

Updating Client Information

// PUT /clientes/{cliente}
$data = [
    'nombre' => 'Juan Carlos',
    'apellidos' => 'Rodríguez Pérez',
    'genero' => 'M',
    'dni_ruc' => '12345678',
    'empresa' => null,
    'email' => '[email protected]', // Updated email
    'telefono' => '+51 999888777', // Updated phone
    'direccion' => 'Nueva Av. Principal 456, Lima', // Updated address
    'referencia' => 'Casa esquina',
    'estado_cliente' => 'Activo'
];

Marking Client as Inactive

// PUT /clientes/{cliente}
$data = [
    // ... all other fields remain the same
    'estado_cliente' => 'Inactivo'
];

Searching Clients

// GET /clientes?search=Rodriguez
// Searches across client fields for matching records

// GET /clientes?search=12345678
// Can search by DNI/RUC

// GET /clientes?search=Constructora
// Can search by company name

Search Functionality

The Cliente model implements a search scope that allows filtering by:
  • Client name (nombre)
  • Last name (apellidos)
  • DNI/RUC identification
  • Company name (empresa)
  • Email address
Search is case-insensitive and uses partial matching.

Error Handling

Deletion Constraints

When attempting to delete a client with related records:
// If client is assigned to expedientes or casos
return back()->with('messageAlert', 
    'Este cliente esta asignado a un expedientes.');
Resolution:
  1. First remove or reassign all expedientes associated with the client
  2. Remove or reassign all casos associated with the client
  3. Then delete the client

Validation Errors

Laravel’s validation will automatically return error messages for:
  • Missing required fields (nombre, apellidos, dni_ruc, estado_cliente)
  • Invalid data formats
  • Type mismatches

Field Descriptions

Required Fields

nombre
string
required
First Name - Client’s given name(s)
apellidos
string
required
Last Names - Client’s family name(s). In Spanish-speaking countries, typically includes both paternal and maternal surnames.
dni_ruc
string
required
DNI/RUC - National identification number. DNI for individuals (8 digits), RUC for companies (11 digits starting with 10 or 20).
estado_cliente
string
required
Client Status - Current status of the client. Common values:
  • Activo - Active client
  • Inactivo - Inactive client
  • Suspendido - Suspended client

Optional Fields

genero
string
Gender - Client’s gender. Common values: M (Masculino), F (Femenino), Otro
empresa
string
Company - Company or organization name if the client represents a legal entity
email
string
Email - Client’s email address for communications
telefono
string
Phone - Contact phone number. Can include country code (e.g., +51 for Peru)
direccion
string
Address - Physical address of the client
referencia
string
Reference - Additional location details or landmarks to help find the address

Relationships

Client to Expedientes

Clients can have multiple expedientes (conciliation case files). The relationship is displayed in the show() method.
// Access client's expedientes
$cliente->expedientes; // Returns collection of related expedientes

Client to Casos

Clients can have multiple casos (legal cases).
// Access client's casos
$cliente->casos; // Returns collection of related casos
  • CasosController - Manages legal cases that reference clients
  • ExpedienteController - Manages expedientes that reference clients
  • Cliente - Main client model at /home/daytona/workspace/source/app/Models/Cliente.php
  • Expedientes - Expediente model for conciliation cases
  • Casos - Case model for legal cases

Best Practices

  1. Always validate DNI/RUC format - Ensure proper format for identification numbers
  2. Check for duplicates - Before creating, search for existing clients with same DNI/RUC
  3. Maintain client status - Update estado_cliente when client relationship changes
  4. Clean up relationships - Before deleting a client, ensure all cases and expedientes are reassigned
  5. Use search functionality - Leverage the search feature to avoid duplicate client entries

Build docs developers (and LLMs) love