Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Route resource destroy not working

Tags:

php

laravel

Here is my form :

<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
              <div class="modal-footer no-border">
                <button type="button" class="btn btn-info" data-dismiss="modal">No</button>
                <button type="submit" class="btn btn-primary">Yes</button>
               <input type="hidden" name="_method" value="DELETE" />

              </div>
              </form>

Here is my controller :

public function destroy($id)
{
    $invoice = Invoice::find($id);
    if(!$invoice){
        return redirect()->route('invoice.index')->with(['fail' => 'Page not found !']);
    }
    $invoice->delete();
    return redirect()->route('invoice.index')->with(['success' => 'Invoice Deleted.']);
}

But it can not delete where is the problem ? How solve this ?

like image 501
Nazmul Hossain Avatar asked Oct 07 '16 11:10

Nazmul Hossain


2 Answers

You need to use POST method for the form and add input element with name _method and value DELETE. Also, add token:

<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
    <input name="_method" type="hidden" value="DELETE">
    {{ csrf_field() }}

    <div class="modal-footer no-border">
        <button type="button" class="btn btn-info" data-dismiss="modal">No</button>
        <button type="submit" class="btn btn-primary">Yes</button>
    </div>
</form>
like image 191
Alexey Mezenin Avatar answered Oct 24 '22 14:10

Alexey Mezenin


I think you must add a hidden input to the form which will contain the method used:

<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
    <input type="hidden" name="_method" value="DELETE" />
</form>

Read more on Laravel documentation about Form method spoofing

like image 29
Mihai Matei Avatar answered Oct 24 '22 16:10

Mihai Matei