Skip to main content

Overview

This page documents the main controllers for managing clients and vehicles in the VIP2CARS system. Both controllers follow Laravel’s resource controller pattern with standard CRUD operations.

ClienteController

Namespace: App\Http\Controllers Location: app/Http/Controllers/ClienteController.php Handles all client-related operations including creating, reading, updating, and deleting client records.

Methods

index()

Display a listing of all clients. Signature:
public function index()
Returns: View clientes.index with all clients Implementation:
public function index()
{
    $clientes = \App\Models\Cliente::all();
    return view('clientes.index', compact('clientes'));
}

create()

Show the form for creating a new client. Signature:
public function create()
Returns: View clientes.create Implementation:
public function create()
{
    return view('clientes.create');
}

store()

Store a newly created client in the database. Signature:
public function store(Request $request)
Parameters:
request
Request
required
HTTP request containing client data
Validation Rules:
nombres
string
required
  • Rules: required|string|max:255
  • Client’s first name(s)
apellidos
string
required
  • Rules: required|string|max:255
  • Client’s last name(s)
nro_documento
string
required
  • Rules: required|string|unique:clientes,nro_documento
  • Document number (must be unique)
correo
string
required
  • Rules: required|email|unique:clientes,correo
  • Email address (must be valid and unique)
telefono
string
required
  • Rules: required|string|max:20
  • Phone number
Returns: Redirect to clientes.index with success message Implementation:
public function store(Request $request)
{
    $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',
    ]);

    \App\Models\Cliente::create($data);

    return redirect()->route('clientes.index')->with('success', 'Cliente creado correctamente.');
}

show()

Display the specified client. Signature:
public function show(string $id)
Parameters:
id
string
required
Client ID (id_cliente)
Returns: View clientes.show with client data Throws: ModelNotFoundException if client not found Implementation:
public function show(string $id)
{
    $cliente = \App\Models\Cliente::findOrFail($id);
    return view('clientes.show', compact('cliente'));
}

edit()

Show the form for editing the specified client. Signature:
public function edit(string $id)
Parameters:
id
string
required
Client ID (id_cliente)
Returns: View clientes.edit with client data Throws: ModelNotFoundException if client not found Implementation:
public function edit(string $id)
{
    $cliente = \App\Models\Cliente::findOrFail($id);
    return view('clientes.edit', compact('cliente'));
}

update()

Update the specified client in the database. Signature:
public function update(Request $request, string $id)
Parameters:
request
Request
required
HTTP request containing updated client data
id
string
required
Client ID (id_cliente)
Validation Rules:
nombres
string
required
  • Rules: required|string|max:255
apellidos
string
required
  • Rules: required|string|max:255
nro_documento
string
required
  • Rules: required|string|unique:clientes,nro_documento,{id},id_cliente
  • Unique except for current client
correo
string
required
  • Rules: required|email|unique:clientes,correo,{id},id_cliente
  • Unique except for current client
telefono
string
required
  • Rules: required|string|max:20
Returns: Redirect to clientes.index with success message Throws: ModelNotFoundException if client not found Implementation:
public function update(Request $request, string $id)
{
    $cliente = \App\Models\Cliente::findOrFail($id);
    
    $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',
    ]);

    $cliente->update($data);

    return redirect()->route('clientes.index')->with('success', 'Cliente actualizado correctamente.');
}

destroy()

Remove the specified client from the database. Signature:
public function destroy(string $id)
Parameters:
id
string
required
Client ID (id_cliente)
Returns: Redirect to clientes.index with success message Throws: ModelNotFoundException if client not found Implementation:
public function destroy(string $id)
{
    $cliente = \App\Models\Cliente::findOrFail($id);
    $cliente->delete();

    return redirect()->route('clientes.index')->with('success', 'Cliente eliminado correctamente.');
}

VehiculoController

Namespace: App\Http\Controllers Location: app/Http/Controllers/VehiculoController.php Handles all vehicle-related operations including creating, reading, updating, and deleting vehicle records.

Methods

index()

Display a listing of all vehicles with their owner information. Signature:
public function index()
Returns: View vehiculos.index with all vehicles (eager-loaded with cliente relationship) Implementation:
public function index()
{
    $vehiculos = \App\Models\Vehiculo::with('cliente')->get();
    return view('vehiculos.index', compact('vehiculos'));
}

create()

Show the form for creating a new vehicle. Signature:
public function create()
Returns: View vehiculos.create with list of all clients Implementation:
public function create()
{
    $clientes = \App\Models\Cliente::all();
    return view('vehiculos.create', compact('clientes'));
}

store()

Store a newly created vehicle in the database. Signature:
public function store(Request $request)
Parameters:
request
Request
required
HTTP request containing vehicle data
Validation Rules:
placa
string
required
  • Rules: required|string|unique:vehiculos,placa
  • License plate (must be unique)
marca
string
required
  • Rules: required|string|max:255
  • Vehicle brand/manufacturer
modelo
string
required
  • Rules: required|string|max:255
  • Vehicle model
anio_fabricacion
numeric
required
  • Rules: required|numeric|digits:4
  • Year of manufacture (must be 4 digits)
id_cliente
integer
required
  • Rules: required|exists:clientes,id_cliente
  • Must reference an existing client
Returns: Redirect to vehiculos.index with success message Implementation:
public function store(Request $request)
{
    $data = $request->validate([
        'placa' => 'required|string|unique:vehiculos,placa',
        'marca' => 'required|string|max:255',
        'modelo' => 'required|string|max:255',
        'anio_fabricacion' => 'required|numeric|digits:4',
        'id_cliente' => 'required|exists:clientes,id_cliente',
    ]);

    \App\Models\Vehiculo::create($data);

    return redirect()->route('vehiculos.index')->with('success', 'Vehículo creado correctamente.');
}

show()

Display the specified vehicle with owner information. Signature:
public function show(string $id)
Parameters:
id
string
required
Vehicle ID (id_vehiculo)
Returns: View vehiculos.show with vehicle data (eager-loaded with cliente relationship) Throws: ModelNotFoundException if vehicle not found Implementation:
public function show(string $id)
{
    $vehiculo = \App\Models\Vehiculo::with('cliente')->findOrFail($id);
    return view('vehiculos.show', compact('vehiculo'));
}

edit()

Show the form for editing the specified vehicle. Signature:
public function edit(string $id)
Parameters:
id
string
required
Vehicle ID (id_vehiculo)
Returns: View vehiculos.edit with vehicle data and list of all clients Throws: ModelNotFoundException if vehicle not found Implementation:
public function edit(string $id)
{
    $vehiculo = \App\Models\Vehiculo::findOrFail($id);
    $clientes = \App\Models\Cliente::all();
    return view('vehiculos.edit', compact('vehiculo', 'clientes'));
}

update()

Update the specified vehicle in the database. Signature:
public function update(Request $request, string $id)
Parameters:
request
Request
required
HTTP request containing updated vehicle data
id
string
required
Vehicle ID (id_vehiculo)
Validation Rules:
placa
string
required
  • Rules: required|string|unique:vehiculos,placa,{id},id_vehiculo
  • Unique except for current vehicle
marca
string
required
  • Rules: required|string|max:255
modelo
string
required
  • Rules: required|string|max:255
anio_fabricacion
numeric
required
  • Rules: required|numeric|digits:4
id_cliente
integer
required
  • Rules: required|exists:clientes,id_cliente
Returns: Redirect to vehiculos.index with success message Throws: ModelNotFoundException if vehicle not found Implementation:
public function update(Request $request, string $id)
{
    $vehiculo = \App\Models\Vehiculo::findOrFail($id);
    
    $data = $request->validate([
        'placa' => 'required|string|unique:vehiculos,placa,'.$id.',id_vehiculo',
        'marca' => 'required|string|max:255',
        'modelo' => 'required|string|max:255',
        'anio_fabricacion' => 'required|numeric|digits:4',
        'id_cliente' => 'required|exists:clientes,id_cliente',
    ]);

    $vehiculo->update($data);

    return redirect()->route('vehiculos.index')->with('success', 'Vehículo actualizado correctamente.');
}

destroy()

Remove the specified vehicle from the database. Signature:
public function destroy(string $id)
Parameters:
id
string
required
Vehicle ID (id_vehiculo)
Returns: Redirect to vehiculos.index with success message Throws: ModelNotFoundException if vehicle not found Implementation:
public function destroy(string $id)
{
    $vehiculo = \App\Models\Vehiculo::findOrFail($id);
    $vehiculo->delete();

    return redirect()->route('vehiculos.index')->with('success', 'Vehículo eliminado correctamente.');
}

Common Patterns

Both controllers use findOrFail() which automatically throws a ModelNotFoundException (404 error) when a record is not found. Laravel handles this exception and returns a 404 page.
All create, update, and delete operations redirect with a success flash message that can be displayed to users:
  • 'Cliente creado correctamente.'
  • 'Cliente actualizado correctamente.'
  • 'Cliente eliminado correctamente.'
  • 'Vehículo creado correctamente.'
  • 'Vehículo actualizado correctamente.'
  • 'Vehículo eliminado correctamente.'
The VehiculoController uses eager loading with with('cliente') to avoid N+1 query problems when displaying vehicle lists with owner information.

Build docs developers (and LLMs) love