I made a user management system with soft deletion and force deletion options. However, I’m having trouble getting the force deletion option to work.
The route:
Route::post('users/{user}/delete', 'UserController@forcedelete');
The relevant controller code:
public function forcedelete(User $user) { $user->forceDelete(); return redirect('users/trash'); }
The view code:
<a href="{{ url('users/'.$user->id.'/delete') }}" onclick="event.preventDefault(); document.getElementById('delete').submit();"> <i class="fa fa-trash-o btn btn-danger btn-xs"></i> </a> <form id="delete" action="{{ url('users/'.$user->id.'/delete') }}" method="POST" style="display: none;"> {{ csrf_field() }} {{ method_field('DELETE') }} </form>
The error that I’m getting is
MethodNotAllowedHttpException in RouteCollection.php line 233:
Why is it not working, and how can I fix it?
Advertisement
Answer
Try placing this route above your other user routes or user resource route. Also you’re trying to use route model binding with a soft deleted model, which won’t work. You need to use the id and delete it manually.
public function forcedelete($id) { User::where('id', $id)->forceDelete(); return redirect('users/trash'); }
Edit: Also delete {{ method_field('DELETE') }}
from your form, since the route method defined is post.