HomeTech PointPHP: How to fix...

PHP: How to fix error “The POST method is not supported for this route. Supported methods: GET, HEAD” Laravel.

Sometimes, you may face an issue with your edit page. When you submit, this error occurs:

How to fix error “The POST method is not supported for this route. Supported methods: GET, HEAD”

You have no idea where that originated, as you are relatively new to Laravel.

routes(web.php):

Route::group([‘middleware’ => ‘auth’], function () {

Route::get(‘/’, ‘[email protected]’);

Route::get(‘/projects/{id}’, ‘[email protected]’);

Route::post(‘/create’,’[email protected]’);

Route::get(‘/create’, ‘[email protected]’);

Route::get(‘/projects/{id}/delete’, ‘[email protected]’);

Route::put(‘/edit’,’[email protected]’);

Route::get(‘/projects/{id}/edit’, ‘[email protected]’);

});

 

Controller:

public function edit($id)

{

return view(‘project.edit’,[

‘project’ => Project::find($id)

]);

}

/**

* Update the specified resource in storage.

*

* @param  \Illuminate\Http\Request  $request

* @param  int  $id

* @return \Illuminate\Http\Response

*/

public function update(Request $request)

{

$project = Project::find($request->id);

$project->project_name = $request->input(‘project_name’);

$project->client = $request->input(‘client’);

$project->description = $request->input(‘description’);

$project->time_span = $request->input(‘time_span’);

$project->text_report = $request->input(‘text_report’);

$project->created_by = $request->input(‘created_by’);

$project->save();

return  redirect(‘/’)->with(‘success’, ‘Project aangepast’);

}

 

There are various ways to handle this situation:

If you insist on using PUT, you can change the form action to POST and add a hidden method field with the value PUT as well as a hidden csrf field (if you’re using blade, simply add @csrf field and method field(‘PUT’) ). The form would then accept the request.

Simply update the form’s route and method to POST. Since you are the one specifying the route and not the resource group, it will function properly.

You may aware that this is not the answer to the OP’s question. However, Google indexed this site #1 when I looked for solutions to this problem. I believe that this will benefit others for this reason.

  • The subsequent error…
  • This route does not support the POST method. Methods supported: GET and HEAD.

caused by failure to delete the routing cache.

php artisan route:cache

add @method(‘PUT’) on the form

Exp:

<form action=”…” method=”POST”>

@csrf 

@method(‘PUT’)

</form>

In your situation, simply executing the command worked like a charm.

php artisan route:clear

You can change the /managers/games/id/push/ link by removing the final slash and it will start working.

$http({

method: ‘POST’,

url: “/managers/games/id/push”,

Possible cause: You haven’t yet upgraded to Laravel 5.8.

The following is how your code will appear in web.php:

Route::post(‘/edit/{id}’,’[email protected]’);

First, we get rid of the random parameter id so that it looks like this:

Route::post(‘/edit’,’[email protected]’);

Second, ditch the @method(‘PUT’) from your form; in this case, we’ll stick with the tried-and-true POST method.

But how can you send that identifier to my method?

The first thing you need to do is create a form input field and give it the hidden property.

A hidden input with the following parameters:

 input type=”hidden”, value=”$project->id”, name=”id”>

You’ll need to retrieve that ID in the update method of your controller.

$id = $request->input(‘id’);

Consequently, you cannot utilise it to determine which work needs updating.

$project = Project::find($id)

//OR

$project = Project::where(‘id’,$id);

The simple way to fix this is to add this to your form.

 

 

 

{{ csrf_field() }}

<input type=”hidden” name=”_method” value=”PUT”>

 

then the update method will be like this :

public function update(Request $request, $id)

{

$project = Project::findOrFail($id);

$project->name = $request->name;

$project->description = $request->description;

$post->save();

}

 

If you are using a Route::group, with a vendor plugin like LaravelLocalization (from MCAMARA), you need to put POST routes outside of this group. I’ve experienced problems with POST routes using this plugin and I did solved right now by putting these routes outside Route::group..

Just name your routes.

Route::post(‘/{page}/articles’, AddArticle::class)->name(‘addArticle’);

Not all of the paths need to be written out, just stick to the standard procedures. ITtutoria.net is also available to help with any problem.

We hope you will find the Solution!!!

Check out: Programming Languages For eLearning App Development

Most Popular

More Articles

How To Spy On iPhone From Android

iPhone and Android are arch-rivals when it comes to smartphones. In...

Different Categories of AI for Executives to Understand

Too often in the media, artificial intelligence is referenced as a...

how to Unpin Someone on snapchat: Android & iPhone

Snapchat is one of the world’s top popular social media apps....

The 7 Best Portable Gaming Systems You Can Buy

Video games are no longer a niche hobby; they're a mainstream...

Read Now

NBA Streams for Watching Live & Recorded Games

If you're getting excited about watching the NBA Streams, then you may be interested in this article on NBA streaming. Games are an integral aspect of life. It is a good idea; if not your thing, then you must think about them as a way to boost...

Most Wanted Breeding and strategic games

Breeding and strategic games are everyone's favorites, as we all like the Talking Tom Cat breeding game and the most popular Android strategic game, Clash Of Clans. In addition, these games are advantageous, as playing strategic games aids in creating businesses and other strategies, as well as...

Cherry MX Switches: Tactile Switch for Notebook Keyboards!

Which mechanical keyboard switch is the best? Cherry MX switches were once considered to be the best alternative. However, Gateron switches are growing in popularity, and many individuals question if they are superior to Cherry MX switches. This blog post will compare and contrast these two types...