Laravel 8可以从数据库中删除:删除方法不是

Laravel 8可以从数据库中删除:删除方法不是,laravel,Laravel,我遇到错误“此路由不支持删除方法。支持的方法:GET、HEAD、POST。”当我想根据他们的Id删除数据库中的某些项目时。以下是我的代码: index.blade.php <form action="{{ $note->id }}" method="post" class="d-inline"> @csrf @method('DELETE') <button type="subm

我遇到错误“此路由不支持删除方法。支持的方法:GET、HEAD、POST。”当我想根据他们的Id删除数据库中的某些项目时。以下是我的代码:

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>
Route::group(['middleware' => 'auth'], function () {
    Route::resource('notes', NoteController::class);
});
public function destroy(Note $note_id)
{
    //
    $userId = Auth::user()->id;
    Note::where(['user_id' => $userId, 'note_id' => $note_id])->delete();
    return $note_id;
}
NoteController.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>
Route::group(['middleware' => 'auth'], function () {
    Route::resource('notes', NoteController::class);
});
public function destroy(Note $note_id)
{
    //
    $userId = Auth::user()->id;
    Note::where(['user_id' => $userId, 'note_id' => $note_id])->delete();
    return $note_id;
}

如何修复此错误,以便从数据库中删除项目?谢谢:)

您需要将操作参数更改为
action=“/notes/{{{$note->id}}”

你没有走那条路线。您正在点击
/{id}

或者使用命名路由
action=“{route('notes.destroy',$note->id)}}

因为您正在使用资源,所以将为store show创建命名路由,销毁并更新所有以名称注释为前缀的路由

因此,如果需要销毁路由,则必须访问名为notes.destroy的/notes/id


您可以使用
php artisan route:list
查看所有已注册的路由及其名称和URI

您需要将操作参数更改为
action=“/notes/{$note->id}”

您没有点击该路线。您点击的是
/{id}

或者使用命名路由
action=“{route('notes.destroy',$note->id)}}

因为您正在使用资源,所以将为store show创建命名路由,销毁并更新所有以名称注释为前缀的路由

因此,如果需要销毁路由,则必须访问名为notes.destroy的/notes/id


您可以使用
php artisan route:list
查看所有已注册的路由及其名称和URI

action=“{{$note->id}}”
应该是
action=“{route('notes.destroy',['id'=>$note->id])}”
action=“{{$note->id}”
应该是
action=“{{route('notes.destroy',['id'=$note->id]>id])}“
谢谢您的回答。你的建议是正确的:D。而且,销毁参数也必须更改为
(note\u id)
,而不是
(note$note\u id)
,谢谢您的回答。你的建议是正确的:D。而且,销毁参数也必须更改为
(note\u id)
,而不是
(note$note\u id)