Laravel 错误- 路由注释/4/编辑不支持 PUT 方法。支持的方法:GET、HEAD

问题描述 投票:0回答:1

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

(blade php) edit-comments.blade.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 routes methods put
1个回答
0
投票

您的

action
标签中的
form
与您需要的
route
不同。尝试删除
edit
路径末尾的
action
,这样它就可以工作了。

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

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

以你可以的形式使用它

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

有了这个,即使您将来在

path
中更改
web.php
,也可以确保您使用的是正确的路线。

© www.soinside.com 2019 - 2024. All rights reserved.