I'm a complete beginner at laravel and am currently making a simple admin panel. I have a grid that shows users (name, email, etc...) and the problem I have is probably stupid but I can't figure it out. I created a controller method for deleting a user:
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect('/admin')->with('success', 'User has been deleted');
}
And defined route as this:
Route::post('/admin/delete/{id}', 'AdminController@destroy')
->middleware('is_admin')
->name('admin.destroy');
and to delete a user in grid, I used form in my view and even setup headers:
<td>
<form href="{{ route('admin.destroy', $user->id)}}" method="post">
@method('DELETE')
@csrf
<input class="btn btn-danger" type="submit" value="Delete" />
</form>
And everytime I press button for deleting a user, I get this:
The DELETE method is not supported for this route. Supported methods: GET, HEAD.
I just can't figure out what I'm doing wrong. I tried changing route type to post but I get the same error.
Your form does not contain an action
, so it will submit it to the same url as it's on, which is only GET/HEAD.
Try this instead:
<form action="{{ route('admin.destroy', $user->id)}}" method="post">
@method('DELETE')
@csrf
<input class="btn btn-danger" type="submit" value="Delete" />
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With