How to use Laravel's delete route?

1

I have the following route:

Route::delete('/{id}', 'PessoasController@delete');

But with the following form I can not access the route:

<form action="/45" method="DELETE">
  <button type="submit">Deletar</button>
</form>

I do not know what I'm missing, maybe I'm missing concepts, I'm a beginner.

    
asked by anonymous 13.01.2017 / 15:35

2 answers

4

First thing ever, not ) create a route so it can get in the way and cause problems, crash routes, etc. in>. A basic example of creating this type of route would be with Route::post (you do not have to use delete only if it's a REST Web Api , but for form use post and get that's enough.) :

Example:

Route:

Route::post('pessoas/delete', 'PessoasController@delete');

Form:

<form action="/pessoas/delete" method="POST">
  <input type="hidden" name="id" value="45" />
  <button type="submit">Deletar</button>
</form>

Minimum example

An example ( is a how-to tip) of routes for Controller can follow the naming of the name controller name , method name and paramentos :

Route::get('pessoas/create', 'PessoasController@create');  // criar registro
Route::get('pessoas/edit/{id}', 'PessoasController@edit'); // editar registro
Route::get('pessoas/view/{id}', 'PessoasController@view'); // ver registro
Route::post('pessoas/store', 'PessoasController@store');   // salvar novo registro
Route::post('pessoas/update', 'PessoasController@update'); // atualizar registro
Route::post('pessoas/delete', 'PessoasController@delete'); // excluir registro

where the relation is:

  • create uses store for gavar log ,
  • edit uses update to change registry , and
  • view uses delete to delete the registry .

This can be simplified, it can be changed, but never use routes with a paragem and only with it, so face to face this is a error , it hinders the functioning of the site by limiting the site not to have simple route names.

    
13.01.2017 / 15:39
2

HTML forms do not support the PUT, PATCH, and DELETE methods.

To get around this, Laravel has a very useful alternative described in its documentation, see #Form Method Spoofing

With this, it would look like this:

<form method="POST" action="sua-rota">
    <input type="hidden" name="_method" value="DELETE">
    {{ csrf_field() }}
    <button type="submit">Deletar</button>
</form>

Verify that your Laravel version allows you to use this method_field function:

<form method="POST" action="sua-rota">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button type="submit">Deletar</button>
</form>
    
13.01.2017 / 16:11