Delete method returns unencumbered page

Asked

Viewed 267 times

0

I use the form below to send the id of the row to be deleted in the table:

<form method="DELETE" action="{{ URL::to('receitas/delete') }}" >

<input type="hidden" name="id" value="{{ $receita->id }}" >

<button type="submit" class="button-deletar" ></button>

</form>

My controller has the following Function:

public function getDestroy($id){

  // delete
  $receita = Receita::find($id);
  $receita->delete();

  // SALVANDO LOG
  $logger = Helper::salvaLog("receitas",$id,"exclusao");    

  // redirect
  Session::flash('message', 'Receita excluída com sucesso!');
  return Redirect::to('receitas');
}

The Routes are like this:

Route::controller('receitas', 'ReceitaController');

Route::delete('receitas/{id}', ReceitaController@getDestroy );

Because returns 404 page not found ( local/receitas/7 )?

2 answers

4

The problem in your form is in how the method was set.

HTML forms in HTML only support the methods POST and GET. In contrast, HTTP has other methods, such as DELETE, PUT and PATH.

Some frameworks, such as Laravel for example, knowing this HTML limitation, implement a field input of the kind hidden to translate the desired request to the correct application path.

In the case of Laravel, when using the resource of Form::open() it will automatically add a hidden with the name _method, which is how Laravel treats this conversion.

That is, to delete your application recipe correctly, you have two choices:

  1. Convert your form and use Form::open()

    {{ Form::open(array('route' => 'receitas', 'method' => 'DELETE')) }}
    
        <input type="hidden" name="id" value="{{ $receita->id }}" >
        <button type="submit" class="button-deletar" ></button>
    
    {{ Form::close() }}
    
  2. Add a hidden by name _method on your form

    <form action="{{ URL::to('receitas') }}" method="POST">
        <input type="hidden" name="_method" value="DELETE" >    
        <input type="hidden" name="id" value="{{ $receita->id }}" >
    
        <button type="submit" class="button-deletar" ></button>
    
    </form>
    
  • Dear gmsantos, I made the changes as you suggested and I still could not delete from the database, but the return is no longer page not found but the url: local/recipes? _method=Delete&id=7

  • @Cleitonhatsu vc used the form of the Laravel or the second option? try to change the method of the form to post, as the answer edited.

-1

The problem is on your route, you just specified:

Route::delete('receitas/{id}', ReceitaController@getDestroy );

When it really should be:

Route::delete('receitas/delete/{id}', ReceitaController@getDestroy );

But you are passing the parameter via DELETE in your HTML/form, and not taking the id as a parameter by the URL, as the route is doing.

That is, you should point to these routes, as per ex:

receitas/delete/12313 <- delete the id recipe "12313"

receitas/delete/12 <- delete the id recipe "12"

  • 2

    In addition to this answer, I would just say that associating a route /receitas/delete/{id} is somewhat redundant, since the verb HTTP DELETE already says what should be done. The ideal would be to make a DELETE /receitas/{id}.

  • Even then the delete route would not be triggered, as the method passage is missing delete in HTML form via _method

Browser other questions tagged

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