dzstr
dzstr

Reputation: 73

PUT method not supported for the route when trying to do an update method

This is what my code looks like

Route:

Route::put('/articles/{$article}', 'ArticlesController@update');

Controller:

 public function update($id){

        $article=Article::find($id);

        $article ->title = request('title');
        $article->excerpt=request('excerpt');
        $article->body=request('body');

        $article->save();

        return redirect('/articles/'. $article->id);


    }

Blade:

 <form method="POST" action="/articles/{{$article->id}}" >
                @csrf
                @method('PUT')

And everytime I try to submit an update I get this:

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

I'm currently stuck on this one.

Upvotes: 1

Views: 284

Answers (3)

Naveed Ali
Naveed Ali

Reputation: 1045

simple way using blade

<form action="/articles/{{$article->id}}" method="POST">
 @method('put')
 @csrf
</form>

Upvotes: 1

Amir Jani
Amir Jani

Reputation: 420

It's better to do:

  • in routing
Route::put('/articles/{id}', 'ArticlesController@update')->name('articles.update');
  • in controller
public function update(Request $request, $id)
{
   // logic
}

don't forget to use Request in controller

  • in blade

it's better to use naming for routes but it might be a problem in your action

   <form method="POST" action="{{ route('articles.update', $article->id) }}">

Upvotes: 1

Bahaeddine
Bahaeddine

Reputation: 119

Try this

<form action="/articles/{{$article->id}}" method="POST">
   <input type="hidden" name="_method" value="PUT">
   <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

and the route

Route::put('/articles/{article}', 'ArticlesController@update');

Upvotes: 4

Related Questions