skip to Main Content

Trying to update a comment using update function i created but when i route the comment i get the error ‘The PUT method is not supported for route comments/4/edit. Supported methods: GET, HEAD.’ – the method is called ‘POST’ and ive called ‘@method(‘PUT’) – ive cleared out my routes cache using the artisan command and still hasnt fixed it- im new to coding in general and i cant seem to figure out the problem – many thanks

(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>

CommentsController

<?php

namespace AppHttpControllers;

use AppHttpRequestsCommentsRequest;
use AppModelsComment;
use AppModelsPhotos;
use IlluminateHttpRedirectResponse;
use IlluminateHttpRequest;

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!');
    }
}

Routing that was used

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

2

Answers


  1. The action in your form tag is different to the route that you need. Try removing the edit at the end of the action path so that it will work.

    Tip : To make creating url easier try adding a name to the routes.

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

    to use it in the form you can just

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

    with this you can ensure that you are using the correct route even if you chage the path in the web.php in the future.

    Login or Signup to reply.
  2. Your route is invalid. you are trying to access PUT comments/4/edit but the there is no such route. Your current route is without /edit. so you can change your route to

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

    Or you can also change your reference target link int the form to /comments/{{$comment->id}} like below:

    <form method="POST" action="/comments/{{$comment->id}}" enctype="multipart/form-data">
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search