Search code examples
phplaravelrouteshttprequest

Deleting a user as admin in Laravel 10: "GET method not supported"


I'm building an admin panel in Laravel 10 where the admin can see a listing of all the users along with a link to delete each individual user. When I click on the delete link i'm getting this error: "The GET method is not supported for route admin/users/2. Supported methods: DELETE." I know it's a very common error but I think I've double checked everything.

Here are my routes:

Route::group(['middleware' => ['auth', 'isadmin']], function () {
    Route::get('/admin/dashboard', [AdminController::class, 'adminDashboard'])
        ->name('admin.dashboard');
    Route::delete('/admin/users/{user}', [AdminController::class, 'destroyUser'])
        ->name('admin.users.destroy');
    Route::redirect('/admin', '/admin/dashboard');
});

My controller:

    public function destroyUser(User $user)
    {
        // dd($user);
        $user->delete;

        return redirect()->back();
    }

My view:

<form action="{{ route('admin.users.destroy', $user) }}"
  method="POST">
  @csrf
  @method('DELETE')
  <a class="text-blue-500 hover:text-blue-700" href="{{ route('admin.users.destroy', $user) }}">Delete</a>
</form>

As you can see, I didn't forget to add @method('DELETE') in the form, I've specified the route method as delete in the router. I tried using a submit type button instead of a link. I tried moving the route outside of the middleware. I also tried clearing the cache multiple times.

Thanks for your help!


Solution

  • Your form is using a link as a submit button. It needs to be a button with the type set to submit.

    <button type="submit" class="text-blue-500 hover:text-blue-700"">Delete</button>