Sometimes, you may face an issue with your edit page. When you submit, this error occurs:
How to fix error “PHP: 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(‘/’, ‘ProjectController@index’); Route::get(‘/projects/{id}’, ‘ProjectController@show’); Route::post(‘/create’,’ProjectController@store’); Route::get(‘/create’, ‘ProjectController@create’); Route::get(‘/projects/{id}/delete’, ‘ProjectController@destroy’); Route::put(‘/edit’,’ProjectController@update’); Route::get(‘/projects/{id}/edit’, ‘ProjectController@edit’); }); |
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}’,’ProjectController@update’); |
First, we get rid of the random parameter id so that it looks like this:
Route::post(‘/edit’,’ProjectController@update’);
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