Search code examples
laravelcrudlaravel-form

The PATCH method is not supported for this route. Supported methods: GET, HEAD


<form action="{{ route('todo.edit',$todoedit->id,'edit') }}" method="POST" class="container">
        @csrf
        @method('PATCH')
        <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" name="title" value="{{$todoedit->title}}">
        </div>
        <div class="form-group">
        <label for="description">Description</label>
        <input type="text" class="form-control" name="description" value="{{$todoedit->description}}">
        </div>
        <button type="submit" class="btn btn-primary form-control">Update</button>
        </form>

Todo Controller:

    public function edit(Request $request,$id)
    { 
        $todo=Todo::find($id);
        $todo->title=$request->title;
        $todo->description=$request->description;
        $todo->save();
        return redirect(route('todo.index'));
    }

I do not know what seems to be the problem, I am doing the CRUD, everything is working but the Update part is not working, it is giving me the error

The PATCH method is not supported for this route. Supported methods: GET, HEAD.

I have tried everything, @method('UPDATE') and PUT and everything but it does not work


Solution

  • Because you write your update function body in edit method in your controller. Do as below:

        public function update(Request $request,$id)
        { 
            $todo=Todo::find($id);
            $todo->title=$request->title;
            $todo->description=$request->description;
            $todo->save();
            return redirect(route('todo.index'));
        }
    

    and in your edit method just return edit view and pass $todo object to that

    Method type of edit is: Get

    Method type of update is: Put or Patch

    You can see this types with simple run php artisan route:list in your terminal.