I'm trying to understand PHP Laravel but can't figure out how to make this put operation.Basically I have my db created with artisan and the table for vehiculos is simple.
Schema::create('vehiculos', function (Blueprint $table) { $table->string('id')->foreign('idFk')->references('id')->on('clientes')->onDelete('cascade'); $table->string('marca'); $table->string('modelo'); $table->year('anio'); $table->string('matricula')->primary(); $table->timestamps(); });
I just want to be able to change the vehicle data, of course cannot change id neither the matricula (license plate), everything goes well except using the PUT operation. Gives me this error: The PUT method is not supported for route vehiculos/7676729/edit. Supported methods: GET, HEAD.
What it confuses me is that I'm doing the exact thing at another class being Client where I update it and works like a charm, here doesn't.
This is my VehiculoController.php
public function update(Request $request, Vehiculo $vehiculo) { // Define las reglas de validación $request->validate(['matricula' => 'required|unique:vehiculos' . $vehiculo->matricula,'id' => 'required' . $vehiculo->id,'marca' => 'required','modelo' => 'required','anio' => 'required', ]); $vehiculo->update($request->all()); return redirect()->route('vehiculos.listar')->with('success', 'Vehiculo actualizado con éxito.'); }
My web.php
Route::get('/', function () { return view('welcome');});Route::get('/clientes', [ClienteController::class, 'index'])->name('clientes.index');Route::resource('clientes', ClienteController::class) ->names(['index' => 'clientes.listar','create' => 'clientes.crear','store' => 'clientes.guardar','show' => 'clientes.ver','edit' => 'clientes.editar','update' => 'clientes.actualizar','destroy' => 'clientes.eliminar', ]);Route::resource('vehiculos', VehiculoController::class) ->names(['index' => 'vehiculos.listar','create' => 'vehiculos.crear','store' => 'vehiculos.guardar','show' => 'vehiculos.ver','edit' => 'vehiculos.editar','update' => 'vehiculos.actualizar','destroy' => 'vehiculos.eliminar', ]);
My edit.blade.php
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Editar vehiculos</title></head><body><h1>Modificar vehiculo</h1> @if (session('success'))<div>{{session('success')}}</div> @endif<form action="{{route('vehiculos.editar', $vehiculo->matricula)}}" method="POST"> @csrf @method("PUT")<label for="matricula" >matricula:</label><input type="text" name="matricula" value="{{$vehiculo->matricula}}" readonly><label for="id">id:</label><input type="text" name="id" value="{{$vehiculo->id}}" readonly><label for="marca">marca:</label><input type="text" name="marca" value="{{$vehiculo->marca}}"><label for="modelo">modelo:</label><input type="text" name="modelo" value="{{$vehiculo->modelo}}"><label for="anio">anio:</label><input type="text" name="anio" value="{{$vehiculo->anio}}"><button type="submit">ENVIAR</button> @if ($errors->all()) @foreach ($errors->all() as $error)<ul><li>{{$error}}</li></ul> @endforeach @endif</form></body>
And my index.blade.php
<h1>Lista de vehiculos</h1> @if(session('success'))<div style="color: green;"> {{ session('success') }}</div> @endif<a href="{{ route('vehiculos.crear') }}">Crear Nuevo vehiculo</a><ul> @foreach ($vehiculos as $vehiculo)<li>{{ $vehiculo->marca }} - {{ $vehiculo->modelo }}<a href="{{ route('vehiculos.ver', $vehiculo -> id) }}">Ver</a><a href="{{ route('vehiculos.editar', $vehiculo -> id) }}">Editar</a><form action="{{ route('vehiculos.eliminar', $vehiculo -> id) }}" method="POST"> @csrf @method('DELETE')<button type="submit">Eliminar</button></form></li> @endforeach</ul>
Also my routes available for vehiculos when I launch artisan route:list
GET|HEAD vehiculos ................................................................ vehiculos.listar › VehiculoController@index POST vehiculos ............................................................... vehiculos.guardar › VehiculoController@store GET|HEAD vehiculos/create ......................................................... vehiculos.crear › VehiculoController@create GET|HEAD vehiculos/{vehiculo} ......................................................... vehiculos.ver › VehiculoController@show PUT|PATCH vehiculos/{vehiculo} ................................................ vehiculos.actualizar › VehiculoController@update DELETE vehiculos/{vehiculo} ................................................. vehiculos.eliminar › VehiculoController@destroy GET|HEAD vehiculos/{vehiculo}/edit ................................................. vehiculos.editar › VehiculoController@edit
Make a PUT request and change the data, for example the year or model of the vehicle and see it change in the database, but whenever I try or change PUT to PATCH gives me the same error.