Laravel 8 CAN DELETE FROM DATABASE : The DELETE method is not

All we need is an easy explanation of the problem, so here it is.

I’ve got the error "The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST." when I want to delete some item in my database base on their’s Id. Here is my code :

index.blade.php

<form action="{{ $note->id }}" method="post" class="d-inline">
    @csrf
    @method('DELETE')
    <button type="submit"
        class="bg-red-500 text-white font-bold px-4 py-2 text-sm uppercase rounded tracking-wider focus:outline-none hover:bg-pink-400">
        Delete</button>
</form>

web.php

Route::group(['middleware' => 'auth'], function () {
    Route::resource('notes', NoteController::class);
});

NoteController.php

public function destroy(Note $note_id)
{
    //
    $userId = Auth::user()->id;
    Note::where(['user_id' => $userId, 'note_id' => $note_id])->delete();
    return $note_id;
}

How can I fix this error so I can delete my item from my database? Thank You 🙂

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

you need to change the action param to action="/notes/{{$note->id}}"

you are not hitting that route . you are hitting /{id}

or use named routes action="{{ route('notes.destroy', $note->id ) }}

since you are using resource will make named routes for store show destroy and update all prefixed with name notes.

so if you need the destroy route you have to access /notes/id named notes.destroy

you can use php artisan route:list to see all of your registered routes with their names and uris

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply