Skip to main content
The client management system allows you to create, view, update, and delete client records. Each client can be associated with multiple vehicles, establishing the foundation for the automotive management workflow.

Overview

Clients are managed through the ClienteController located at app/Http/Controllers/ClienteController.php:7. The system uses the Cliente model with a custom primary key id_cliente and stores data in the clientes table.

Model Structure

The Cliente model includes the following fields:

nombres

Client’s first name(s) - Required, string, max 255 characters

apellidos

Client’s last name(s) - Required, string, max 255 characters

nro_documento

Document number - Required, unique, string

correo

Email address - Required, unique, valid email format

telefono

Phone number - Required, string, max 20 characters

id_cliente

Primary key - Auto-generated

CRUD Operations

Listing Clients

Retrieve all clients from the database. Route: GET /clientes Controller Method: ClienteController::index() at line 12
public function index()
{
    $clientes = \App\Models\Cliente::all();
    return view('clientes.index', compact('clientes'));
}

Creating a Client

Route: GET /clientes/createController Method: ClienteController::create() at line 18Displays the client creation form with all required fields.
Route: POST /clientesController Method: ClienteController::store() at line 23Validates and stores a new client in the database.
$data = $request->validate([
    'nombres' => 'required|string|max:255',
    'apellidos' => 'required|string|max:255',
    'nro_documento' => 'required|string|unique:clientes,nro_documento',
    'correo' => 'required|email|unique:clientes,correo',
    'telefono' => 'required|string|max:20',
]);
Success Response: Redirects to clientes.index with success message “Cliente creado correctamente.”

Viewing a Client

Route: GET /clientes/{id} Controller Method: ClienteController::show() at line 38
public function show(string $id)
{
    $cliente = \App\Models\Cliente::findOrFail($id);
    return view('clientes.show', compact('cliente'));
}
The system uses findOrFail() which automatically returns a 404 error if the client doesn’t exist.

Updating a Client

Route: GET /clientes/{id}/editController Method: ClienteController::edit() at line 44Displays the client edit form pre-filled with existing data.
Route: PUT/PATCH /clientes/{id}Controller Method: ClienteController::update() at line 50Validates and updates an existing client record.
$data = $request->validate([
    'nombres' => 'required|string|max:255',
    'apellidos' => 'required|string|max:255',
    'nro_documento' => 'required|string|unique:clientes,nro_documento,'.$id.',id_cliente',
    'correo' => 'required|email|unique:clientes,correo,'.$id.',id_cliente',
    'telefono' => 'required|string|max:20',
]);
The unique validation rules exclude the current client ID to allow updating the same record without triggering uniqueness errors.
Success Response: Redirects to clientes.index with success message “Cliente actualizado correctamente.”

Deleting a Client

Route: DELETE /clientes/{id} Controller Method: ClienteController::destroy() at line 67
public function destroy(string $id)
{
    $cliente = \App\Models\Cliente::findOrFail($id);
    $cliente->delete();
    
    return redirect()->route('clientes.index')
        ->with('success', 'Cliente eliminado correctamente.');
}
Deleting a client may affect associated vehicles. Ensure proper foreign key constraints or cascade rules are configured in your database.

Relationships

The Cliente model has a one-to-many relationship with vehicles defined at app/Models/Cliente.php:20:
public function vehiculos()
{
    return $this->hasMany(Vehiculo::class, 'id_cliente', 'id_cliente');
}
This means:
  • A single client can own multiple vehicles
  • Access a client’s vehicles using $cliente->vehiculos
  • The relationship is keyed by id_cliente on both tables

Validation Rules

On Create

FieldRulesDescription
nombresrequired, string, max:255Client’s first name(s)
apellidosrequired, string, max:255Client’s last name(s)
nro_documentorequired, string, unique:clientes,nro_documentoUnique document number
correorequired, email, unique:clientes,correoUnique email address
telefonorequired, string, max:20Phone number

On Update

Update validation is identical to create validation, except unique rules exclude the current record:
  • unique:clientes,nro_documento,{id},id_cliente
  • unique:clientes,correo,{id},id_cliente

Route Protection

All client management routes are protected by middleware defined in routes/web.php:12:
Route::middleware(['auth', 'verified'])->group(function () {
    Route::resource('clientes', ClienteController::class);
});
Users must be authenticated AND have verified their email address to access client management features.

RESTful Resource Routes

The Route::resource() declaration automatically creates these routes:
MethodURIActionRoute Name
GET/clientesindexclientes.index
GET/clientes/createcreateclientes.create
POST/clientesstoreclientes.store
GET/clientes/showclientes.show
GET/clientes//editeditclientes.edit
PUT/PATCH/clientes/updateclientes.update
DELETE/clientes/destroyclientes.destroy

Build docs developers (and LLMs) love