1

In my routes file, I have

  Route::delete('events/{events}', ['as' => 'events_delete', 'uses' => 'Admin\EventsController@destroy'] );

In my view file I have

<a href="{!! route('events_delete', ['id' => $event->id ]) !!}" type="button" class="btn btn-sm btn-danger"><em class="fa fa-trash"></em></a>

This does not work. When I change the route to

  Route::get('events/{events}', ['as' => 'events_delete', 'uses' => 'Admin\EventsController@destroy'] );

it does work. However I don't like the idea of using a GET verb to delete items instead of the DELETE verb. It feels like a trick...

How can I change the form code to make sure it sends a DELETE verb?

Solution 1 (from TheFallen): with DELETE VERB in routes file

<form action="{!! route('events_delete', ['id' => $event->id ]) !!}" method="POST">
   {{ method_field('DELETE') }}
   {{ csrf_field() }}
   <button class="btn btn-danger btn-sm" type="submit"><em class="fa fa-trash"></em></button>
</form>

Solution 2: with GET VERB in routes file

<a href="{!! route('events_delete', ['id' => $event->id ]) !!}" type="button" class="btn btn-sm btn-danger"><em class="fa fa-trash"></em></a>

2 Answers 2

3

You have to make a delete request to use the route this way, which you can do with a form, otherwise with the anchor you're making a get request.

If you already don't have the laravelcollective/html package install it from composer to use the forms facade. Then you can make the request like this:

    {!! Form::open(['method' => 'DELETE', 'route' => $yourRoute]) !!}
    {!! Form::submit('Delete') !!}
    {!! Form::close() !!}

EDIT:

Without the forms facade:

<form action="{{ $yourRoute }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <button class="btn btn-danger btn-sm" type="submit"><em class="fa fa-trash"></em></button>
</form>
Sign up to request clarification or add additional context in comments.

4 Comments

Is there no other way then using the forms facade? I haven't used it since there must be a reason Laravel decided not to take it longer part of the core Laravel system.
Your suggestion works. I have updated the original post to include the 2 solutions I see working.
One quick question though, seems like your suggestion (without forms facade) results in the trash (delete) icon to be put on a separate line, not next to the other icons. Any idea how I can assure the icon is next to the others?
@wiwa1978 if you have Bootstrap CSS try adding class form-inline to the form tag: <form class="form-inline">.
1

That will produce a GET request, therefore it will not match Route::delete

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form.

Refer: https://laravel.com/docs/master/routing#form-method-spoofing

To call the delete route, you have to implement using jquery

<a eventid="{{$event->id}}" href="#" type="button" class="btn btn-sm btn-danger"><em class="fa fa-trash"></em></a>

$(document).on("click",".anchorclass",function(e){
e.preventDefault();
if(!confirm("Are you sure?")) return;

 $.ajax({
            type: "DELETE",
            url: 'events/'+$(this).attr("eventid"),
            success: function(data) {
                //Process results
            }
        });

}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.