How to use Laravel delete route?

Asked

Viewed 4,878 times

1

I have the following route:

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

But with the following form I cannot access the route:

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

I don’t know what I’m missing, maybe I lack concepts, I’m beginner.

3 answers

5


First thing ever, (by no means) create a route so this can disturb the others and cause problems, crash routes, etc... A basic example in creating this type of route would be with Route::post (don’t need to use delete only if it is a REST Web Api, but for form use post and get that is 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>

Minimal example

An example (is a hint of how it could be) of routes for Controller may follow the nomenclature of controller name, method name and vestments:

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 relationship is:

  • create uses store for record,
  • edit uses update for change registration, and
  • view uses delete for delete the record.

This can all be simplified, it can be changed, but, never use routes with a flag and only with it, right away this is a error, hinders the functioning of the site by limiting the site to have no simple route names.

3

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

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

With that, I’d be like this:

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

Make sure your Laravel version allows you to use this function method_field:

<form method="POST" action="sua-rota">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button type="submit">Deletar</button>
</form>
  • Nice, but I’m having second thoughts about csrf_field is it possible to use without keys? because I am using angular

  • I advise not to use without key. But, if really necessary, you can specify the routes that did not pass the CSRF check. https://laravel.com/docs/5.3/csrf#csrf-excluding-uris

  • I meant the keys {{ that conflict with the angular and generate nothing

  • Which template engine you are using, Blade or Angularjs?

  • I’m using only Angular 1 Carlos

  • You need to pass for the Angulajs o csrf_field somehow. The documentation itself recommends using the meta tag in the header https://laravel.com/docs/5.3/csrf#csrf-x-csrf-token

  • Thanks I’ll study about helped me

  • Carlos figured out how to solve used <?php echo csrf_field(); ?>

Show 3 more comments

0

Voce can use the route with the post() in post method form or get() in links to delete , there is only to lynch by the name la in input through the lib body-parser, easier

Browser other questions tagged

You are not signed in. Login or sign up in order to post.