Laravel 错误 - route comments/4/edit 不支持 PUT 方法。支持的方法:GET、HEAD

Laravel Error- The PUT method is not supported for route comments/4/edit. Supported methods: GET, HEAD

提问人:2Thingz 提问时间:4/26/2023 更新时间:4/26/2023 访问量:418

问:

尝试使用我创建的更新函数更新注释,但是当我路由注释时,我收到错误“路由注释/4/edit 不支持 PUT 方法。支持的方法:GET、HEAD。 - 该方法称为“POST”,我称为“@method('PUT') - 我使用 artisan 命令清除了我的路由缓存,但仍然没有修复它 - 我是一般编码的新手,我似乎无法弄清楚问题 - 非常感谢

(刀片 php) 编辑评论.刀片.php

<x-layout>
    <div
    class="bg-gray-50 border border-gray-200 p-10 rounded max-w-lg mx-auto mt-24"
    >
    <header class="text-center">
        <h2 class="text-2xl font-bold uppercase mb-1">
            Edit Comment
        </h2>
    </header>

    <form method="POST" action="/comments/{{$comment->id}}/edit" enctype="multipart/form-data">
        @csrf
        @method('PUT')
        <div class="mb-6">
            <label
                for="author"
                class="inline-block text-lg mb-2"
                >Name</label
            >
            <input
                type="text"
                class="border border-gray-200 rounded p-2 w-full"
                name="author"
                value="{{$comment->author}}"
            />

            @error('author')
                <p class="text-red-500 text-xs mt-1">{{$message}} </p>
            @enderror
        </div>

        <div class="mb-6">
            <label for="text" class="inline-block text-lg mb-2"
                >Comment</label
            >
            <input
                type="text"
                class="border border-gray-200 rounded p-2 w-full"
                name="text"
                value="{{$comment->text}}"
            />

            @error('text')
            <p class="text-red-500 text-xs mt-1">{{$message}} </p>
            @enderror
        </div>

        <div class="mb-6">
            <button
                class="bg-laravel text-white rounded py-2 px-4 hover:bg-black"
            >
                Update Comment
            </button>

            <a href="/" class="text-black ml-4"> Back </a>
        </div>
    </form>
    </div>
    </x-layout>

评论控制器

<?php

namespace App\Http\Controllers;

use App\Http\Requests\CommentsRequest;
use App\Models\Comment;
use App\Models\Photos;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class CommentsController extends Controller
{
    //Store Comments
    public function store(Photos $photo, CommentsRequest $request):RedirectResponse {
        $data = $request->validated();
        $comment = new Comment();

        $comment->photos_id = $photo->id;
        $comment->author = $data['author'];
        $comment->text = $data['text'];
        $comment->save();

        return back()->with('message', 'Comment uploaded successfully!');
    }

    //Edit Comments form
    public function edit(Comment $comment) {
        return view('edit-comments', ['comment' => $comment]);
    }

    //update Comments
    public function update(Request $request, Comment $comment) {
        $data = $request->validated([
            'author' => 'required',
            'text' => 'required'
        ]);

        $comment->create($data);


        return back()->with('message', 'Comment updated successfully!');
    }

    //Delete Comments
    public function destroy(Comment $comment, Photos $photo):RedirectResponse {
        //if($photo->user_id != auth()->id()) {
        //    abort(403, 'Unauthorised Action');
        //}
        $comment->delete();
        return back()->with('message', 'Comment deleted successfully!');
    }
}

使用的路由

//Update comments
Route::put('/comments/{comment}',
[CommentsController::class, 'update'])->middleware('auth');
php laravel 路由 方法

评论


答:

0赞 xenooooo 4/26/2023 #1

您的标签中的标签与您需要的标签不同。尝试删除路径末尾的 ,以便它正常工作。actionformrouteeditaction

提示:为了使创建网址更容易,请尝试为路由添加名称。

Route::put('/comments/{comment}',[CommentsController::class, 'update'])
    ->name('comments.update')
    ->middleware('auth');

要以形式使用它,您可以只是

<form action="{{ route('comments.update', ['comment' => $comment->id]) }}">...</form>

有了这个,您可以确保您使用正确的路线,即使您将来也会使用正确的路线。pathweb.php

评论

0赞 2Thingz 4/26/2023
我这样做了,但仍然收到错误“路由注释/8 不支持 PUT 方法。支持的方法:POST,DELETE。 - 也感谢您的提示
0赞 Muhammad Dyas Yaskur 4/26/2023 #2

您的路由无效。您正在尝试访问 PUT,但没有这样的路由。您当前的路由没有 .因此,您可以将路线更改为comments/4/edit/edit

//Update comments
Route::put('/comments/{comment}/edit',
[CommentsController::class, 'update'])->middleware('auth');

或者,您也可以将表单中的参考目标链接更改为如下所示:/comments/{{$comment->id}}

<form method="POST" action="/comments/{{$comment->id}}" enctype="multipart/form-data">

评论

0赞 2Thingz 4/26/2023
我这样做了,但仍然收到错误“路由注释/8 不支持 PUT 方法。支持的方法:POST、DELETE。