Laravel Route :: resource

1

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.

    
asked by Chefito 19.11.2018 в 19:15
source

1 answer

1

As far as I understand it can not be done simply with a link, first by the fact that you must specify that it is a delete method (which can not even be done directly by the method of the form if not by an extra variable sent in the form that reads laravel) and second by the use of csrf, so you must do it either with a form using the helpers or with ajax.

example with form:

<form action="{{ route('personas.destroy', ['persona'=>$persona]) }}" method="POST">
    @method('DELETE')
    @csrf
</form>

with ajax, keeping your link style would be something like this (you must see how to pass the url of each link to ajax):

$.ajax({
    url: url,
    type: 'POST', 
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
   data: {
      _method: 'delete',
   },
    success: function(result) {
        // algo de lógica de ser necesaria, como mostrar un success.
    },
});

I wrote the Ajax by heart, but it should work.

Example and explanation in the documentation: link

    
answered by 20.11.2018 / 02:01
source