Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - DELETE method is not support for a delete route

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.

like image 877
Bernard Polman Avatar asked Dec 13 '22 12:12

Bernard Polman


1 Answers

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>
like image 83
PtrTon Avatar answered Dec 18 '22 00:12

PtrTon