If you really want to visit the destroy action on delete route by HTML, then there is an approach to use HTTP Method Spoofing which means that you could visit a delete HTTP method by adding a hidden input named _method
with the value of `"DELETE". Same way can be used for "PUT" and "PATCH" HTTP method.
Below is a sample for DELETE method.
<form action="/tasks/5" method="POST">
<input type="hidden" name="_method" value="DELETE">
</form>
will get the route
DELETE /tasks/{id} destroy tasks.destroy
if you use laravel collective, you can write this way in your views.
{!! Form::open(['url' => '/tasks/'.$cat->id, 'method' => 'delete']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
You need to send a DELETE
request instead of a GET
request. You can't do that with a link, so you have to use an AJAX request or a form.
Here is the generic form method:
<form action="{{ URL::route('user.destroy', $members['id'][$i]) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button>Delete User</button>
</form>
If you're using Laravel 5.1 or later then you can use Laravel's built-in helpers to shorten your code:
<form action="{{ route('user.destroy', $members['id'][$i]) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button>Delete User</button>
</form>
If you're using Laravel 5.6 or later then you can use the new Blade directives to shorten your code even further:
<form action="{{ route('user.destroy', $members['id'][$i]) }}" method="POST">
@method('DELETE')
@csrf
<button>Delete User</button>
</form>
You can read more about method spoofing in Laravel here.
My, non-ajax version. I use it in dropdowns (bootstrap) in resource list (datatables as well). Very short and universal.
Global jQuery method:
$('.submit-previous-form').click(function (e) {
e.preventDefault();
$($(this)).prev('form').submit();
});
And then we can use everywhere something like this:
{{ Form::open(['route' => ['user.destroy', $user], 'method' => 'delete']) }} {{ Form::close() }}
<a href="#" class="dropdown-item submit-previous-form" title="Delete user"><i class="icon-trash"></i> Delete him</a>
Recommend: It's easy to integrate with confirms scripts for example swal.
If you're looking to do this via a regular link instead of through AJAX or another type of form request you can set up a special route that will respond to a normal GET
request:
In your routes, define this in addition to the resource:
Route::get('user/{site}/delete', ['as' => 'user.delete', 'uses' => 'UserController@destroy']);
In your view:
<a href="{{ route('user.delete', $user->id) }}">Delete this user</a>
In your controller:
public function destroy(User $user)
{
$user->delete();
return redirect()->route('users.index');
}
If you want to use a link, you can use a library I have created that lets people make links that behave like POST, DELETE... calls.
https://github.com/Patroklo/improved-links
If we need to use an anchor to trigger the destroy route, and we don't want to use ajax, we can put a form inside our link, and submit the form using the onclick
attribute:
<a href="javascript:void(0);" onclick="$(this).find('form').submit();" >
<form action="{{ url('/resource/to/delete') }}" method="post">
<input type="hidden" name="_method" value="DELETE">
</form>
</a>