Overview
TheClienteController 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 modelExpedientes- 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 (usesrequest('search') for search query)
Returns: View clientes.index with clients
Response Data:
Paginated collection of clients (5 per page)
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: Viewclientes.create
Code Example:
clientes.create
store()
Creates a new client with validation. Purpose: Store a new client in the database with comprehensive validation Request Parameters:Client’s first name
Client’s last name(s)
Client’s gender (optional)
Client’s DNI or RUC identification number
Company name if client represents an organization (optional)
Client’s email address (optional)
Client’s phone number (optional)
Client’s physical address (optional)
Address reference or additional notes (optional)
Client status (e.g., Active, Inactive)
clientes.index with success message
Success Message: “Cliente registrado correctamente.”
Code Example:
clientes.store
show()
Displays detailed information for a specific client. Purpose: Show complete client details including related expedientes Parameters:Client model instance (route model binding)
clientes.cliente with client data and expedientes
Response Data:
The client instance with all details
All expedientes in the system (for relationship display)
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:Client model instance (route model binding)
clientes.edit with client data
Response Data:
The client instance to edit
clientes.edit
update()
Updates an existing client with validation. Purpose: Update client information in the database Parameters:Client model instance (route model binding)
Client’s first name
Client’s last name(s)
Client’s gender (optional)
Client’s DNI or RUC identification number
Company name (optional)
Client’s email address (optional)
Client’s phone number (optional)
Client’s physical address (optional)
Address reference (optional)
Client status
clientes.index with success message
Success Message: “Permiso actualisado correctamente.”
Code Example:
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:Client model instance (route model binding)
- Success: Redirect to
clientes.indexwith success message - Error: Back with alert message if constraints exist
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)
Creating a Corporate Client
Updating Client Information
Marking Client as Inactive
Searching Clients
Search Functionality
TheCliente model implements a search scope that allows filtering by:
- Client name (nombre)
- Last name (apellidos)
- DNI/RUC identification
- Company name (empresa)
- Email address
Error Handling
Deletion Constraints
When attempting to delete a client with related records:- First remove or reassign all expedientes associated with the client
- Remove or reassign all casos associated with the client
- 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
First Name - Client’s given name(s)
Last Names - Client’s family name(s). In Spanish-speaking countries, typically includes both paternal and maternal surnames.
DNI/RUC - National identification number. DNI for individuals (8 digits), RUC for companies (11 digits starting with 10 or 20).
Client Status - Current status of the client. Common values:
Activo- Active clientInactivo- Inactive clientSuspendido- Suspended client
Optional Fields
Gender - Client’s gender. Common values:
M (Masculino), F (Femenino), OtroCompany - Company or organization name if the client represents a legal entity
Email - Client’s email address for communications
Phone - Contact phone number. Can include country code (e.g., +51 for Peru)
Address - Physical address of the client
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 theshow() method.
Client to Casos
Clients can have multiple casos (legal cases).Related Controllers
- CasosController - Manages legal cases that reference clients
- ExpedienteController - Manages expedientes that reference clients
Related Models
- 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
- Always validate DNI/RUC format - Ensure proper format for identification numbers
- Check for duplicates - Before creating, search for existing clients with same DNI/RUC
- Maintain client status - Update
estado_clientewhen client relationship changes - Clean up relationships - Before deleting a client, ensure all cases and expedientes are reassigned
- Use search functionality - Leverage the search feature to avoid duplicate client entries