Delete method returns page not found

0

I use the form below to send the id of the line 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');
}

Routing looks like this:

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

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

Why does page 404 not found ( local/receitas/7 ) return?

    
asked by anonymous 07.11.2014 / 00:13

2 answers

4

The problem with your form is how method has been set.

HTML forms only support 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 input field of type hidden to translate the desired request to the correct application route.

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

That is, to properly delete the revenue from your application, you have two choices:

  • 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() }}
    
  • Add a hidden with 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>
    
  • 07.11.2014 / 14:59
    -1

    The problem is in your route, you just specified:

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

    When in fact it should be:

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

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

    That is, you should point to these routes, such as eg:

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

    receitas/delete/12

    07.11.2014 / 11:53