Overview
Vehicles are managed through theVehiculoController located at app/Http/Controllers/VehiculoController.php:7. The system uses the Vehiculo model with a custom primary key id_vehiculo and stores data in the vehiculos table.
Model Structure
TheVehiculo model includes the following fields:
placa
License plate number - Required, unique, string
marca
Vehicle brand/make - Required, string, max 255 characters
modelo
Vehicle model - Required, string, max 255 characters
anio_fabricacion
Manufacturing year - Required, numeric, exactly 4 digits
id_cliente
Foreign key to cliente - Required, must exist in clientes table
id_vehiculo
Primary key - Auto-generated
CRUD Operations
Listing Vehicles
Retrieve all vehicles with their associated client information. Route:GET /vehiculos
Controller Method: VehiculoController::index() at line 12
The
with('cliente') method eager loads the client relationship to prevent N+1 query problems when displaying vehicle lists.Creating a Vehicle
Create Form
Create Form
Route:
GET /vehiculos/createController Method: VehiculoController::create() at line 18Displays the vehicle creation form with a dropdown list of all available clients.Store Vehicle
Store Vehicle
Route: Success Response: Redirects to
POST /vehiculosController Method: VehiculoController::store() at line 24Validates and stores a new vehicle in the database.vehiculos.index with success message “Vehículo creado correctamente.”Viewing a Vehicle
Route:GET /vehiculos/{id}
Controller Method: VehiculoController::show() at line 39
The client relationship is eager loaded to display owner information on the vehicle detail page.
Updating a Vehicle
Edit Form
Edit Form
Route:
GET /vehiculos/{id}/editController Method: VehiculoController::edit() at line 45Displays the vehicle edit form pre-filled with existing data and a dropdown of available clients.Update Vehicle
Update Vehicle
Route: Success Response: Redirects to
PUT/PATCH /vehiculos/{id}Controller Method: VehiculoController::update() at line 52Validates and updates an existing vehicle record.vehiculos.index with success message “Vehículo actualizado correctamente.”Deleting a Vehicle
Route:DELETE /vehiculos/{id}
Controller Method: VehiculoController::destroy() at line 69
Relationships
TheVehiculo model has a many-to-one relationship with clients defined at app/Models/Vehiculo.php:20:
- Each vehicle belongs to exactly one client
- Access a vehicle’s owner using
$vehiculo->cliente - The relationship is keyed by
id_clienteon both tables - The foreign key constraint ensures referential integrity
Validation Rules
On Create
| Field | Rules | Description |
|---|---|---|
placa | required, string, unique:vehiculos,placa | Unique license plate number |
marca | required, string, max:255 | Vehicle make/brand |
modelo | required, string, max:255 | Vehicle model |
anio_fabricacion | required, numeric, digits:4 | 4-digit manufacturing year (e.g., 2024) |
id_cliente | required, exists:clientes,id_cliente | Must reference an existing client |
On Update
Update validation is identical to create validation, except the unique rule for
placa excludes the current record:unique:vehiculos,placa,{id},id_vehiculo
Manufacturing Year Validation
Thedigits:4 rule ensures:
- Exactly 4 numeric characters
- Valid examples: 2024, 1998, 2015
- Invalid examples: 24 (too short), 20245 (too long), 202A (not numeric)
Foreign Key Constraint
Theexists:clientes,id_cliente validation ensures data integrity:
- Checks that the provided client ID exists in the
clientestable - Prevents orphaned vehicle records
- Returns a validation error if the client doesn’t exist
Route Protection
All vehicle management routes are protected by middleware defined inroutes/web.php:12:
RESTful Resource Routes
TheRoute::resource() declaration automatically creates these routes:
| Method | URI | Action | Route Name |
|---|---|---|---|
| GET | /vehiculos | index | vehiculos.index |
| GET | /vehiculos/create | create | vehiculos.create |
| POST | /vehiculos | store | vehiculos.store |
| GET | /vehiculos/ | show | vehiculos.show |
| GET | /vehiculos//edit | edit | vehiculos.edit |
| PUT/PATCH | /vehiculos/ | update | vehiculos.update |
| DELETE | /vehiculos/ | destroy | vehiculos.destroy |
Eager Loading Performance
The controller uses eager loading (
with('cliente')) in the index() and show() methods to optimize database queries and prevent N+1 query problems.