There is a problem with Laravel, with Route::resource
I do not know how to solve.
I create the driver from artisan, like this:
php artisan make:controller PersonaController --model=Persona
In routes / web.php I have
Route::resource('personas', 'PersonaController');
The list of routes is like this:
+--------+-----------+---------------------------+-------------------+-------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+---------------------------+-------------------+-------------------------------------------------+--------------+
| | GET|HEAD | / | entrada | App\Http\Controllers\MainController@raiz | web |
| | GET|HEAD | api/user | | Closure | aut:api |
| | GET|HEAD | personas | personas.index | App\Http\Controllers\PersonaController@index | web |
| | POST | personas | personas.store | App\Http\Controllers\PersonaController@store | web |
| | GET|HEAD | personas/create | personas.create | App\Http\Controllers\PersonaController@create | web |
| | GET|HEAD | personas/{persona} | personas.show | App\Http\Controllers\PersonaController@show | web |
| | PUT|PATCH | personas/{persona} | personas.update | App\Http\Controllers\PersonaController@update | web |
| | DELETE | personas/{persona} | personas.destroy | App\Http\Controllers\PersonaController@destroy | web |
| | GET|HEAD | personas/{persona}/edit | personas.edit | App\Http\Controllers\PersonaController@edit | web |
+--------+-----------+---------------------------+-------------------+-------------------------------------------------+--------------+
The problem appears with the show and destroy methods, which have the same URI.
For the show method (to see the data of a person) I created (using the name of the route), the following link:
<a href="{{ route('personas.show', ['persona'=>$persona]) }}">
VER
</a>
This works perfectly.
The problem is that to create the link to delete, I do it like this:
<a href="{{ route('personas.destroy', ['persona'=>$persona]) }}">
BORRAR
</a>
But since the path with the name " personas.destroy
" has the same URI as the path with the name " personas.show
", instead of sending me the destroy method of the controller, it sends me to the show method.
I suppose that, in principle, there should not be two methods with the same URI but, since there are, I imagine there must be a solution.
I thought that, probably, the deletion method should be launched with the HTTP DELETE method, but in the documentation there is nothing about how to set the HTTP method in the helper route()
.
Come on, that would be great if you told me the trick. After all, since Laravel offers the option of Route::resource
, I guess this
It should be planned, but I've been searching the Internet for hours and I can not find anything.
HELP, PLEASE. SOS.