0

I have a weird problem with laravel. So this error appears after I click on the edit button

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

And when I refresh the page it just shows me a form that should be shown. So it means the code is right... right?

Edit.Blade:

<form action="{{ route('categories.update', $category->slug) }}" method="post">
    @csrf
    @method('put')

    <div class="form-group">
        <label for="name">Category</label>
        <input type="text" name="name" class="form-control" value="{{ $category->name }}">
    </div>

    <div class="form-group">
        <button type="submit" name="btn-updateCategory" class="btn btn-block btn-success">Update category</button>
    </div>
</form>

This $category->slug part is not a problem I am sure.. :)

Also when I refresh page and error is gone also my controller does its job (updates category).

It also happened on other project and I thought it is my bad and since I did not know how to solve it and didn't find any mistakes in code, started that project from scratch and finished it without encountering this problem.

EDIT

Here is my route:list https://prnt.sc/tvjw2i

And here is my web.php

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function(){
   Route::resource('/posts', 'PostsController');
   Route::resource('/categories', 'CategoriesController');
});

Problem solved: Nobody, including me, realised we are looking at the wrong form. Since error appears when I try to go on the update page and not after it, an error was on index.blade.php not on edit.blade.php.

Sead Silajdzic
  • 298
  • 1
  • 4
  • 21
  • Hey Sead, welcome to SO! Are you able to post the relevant content in your `routes/web.php`? Also - if you run `php artisan route:list` in your project root, do you see the route listed? – FullStackOfPancakes Aug 06 '20 at 22:43
  • Also, make up your mind. :D It seems your route in `routes/web.php` for `categories.update` takes `GET` method. Your form's `method` is `POST` while another `@method` directive is set to `PUT`. Since, you're updating something, you probably want `PUT`; change route method in `web.php`. This how you send `PUT` request from view - https://stackoverflow.com/a/28144127/10625611 – Qumber Aug 07 '20 at 05:58
  • @Qumber hello, I have also tried PUT and the same error comes on screen this was just left from the last try :D I usually use PUT – Sead Silajdzic Aug 07 '20 at 07:43
  • @UkraineInTheMembrane there is image of my route list. I see all of them and on update is PUT|PATCH Also u can see here https://prnt.sc/tvjw2i – Sead Silajdzic Aug 07 '20 at 07:44

1 Answers1

0

It’s the issue with the route, please show us your web.php file. If you are not registering route as resource then it should look like this

//route for categories update

Route::post(categories/{id}/update,’CategoriesController@update’);

Or if you are using resource controller

route::resource(‘categories’,CategoriesController’);

Please check this.

Qumber
  • 13,130
  • 4
  • 18
  • 33