id)}}" method="DELETE">

Laravel resource resource not working

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 he can not delete, where is the problem? How to solve this?

+4
source share
3 answers

You need to use the method POSTfor the form and add an element inputwith a name _methodand value DELETE. Also add the 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>
+5
source

I think you should add hidden input to the form that 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 the Laravel documentation on Methods of Form Form

+1
source

PUT DELETE, , HTML ( ) POST GET.

:

{!! method_field('DELETE') !!}

, :

<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
    {!! method_field('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>
    </div>
</form>
0
source

Source: https://habr.com/ru/post/1657057/


All Articles