Martin Mbae
Martin Mbae

Reputation: 1266

Why is Post method giving the error MethodNotAllowedHttpException in Laravel

I am trying to submit a form in Laravel but I am getting the error The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.

I have tried the suggestions in post method in laravel give MethodNotAllowedHttpException but none is working. Here is my code.

<div class="row" style="background: #ffffff;">
  <div class="col-lg-12 col-md-12 col-sm-12" style="background: white; margin: 10px">
    <form method="post" action="{{ route('companies.update',[$company->id]) }}">
      {{ csrf_field() }}

      <input type="hidden" name="method" value="put">

      <div class="form-group">
        <label for="company.name">Name <span class="required">*</span> </label>
        <input placeholder="Enter name" id="company-name" required name="description" spellcheck="false" class="form-control" value="{{ $company->name }}" />


      </div>

      <div class="form-group">
        <label for="company-content">Description</label>
        <textarea placeholder="Enter Description" style="resize: vertical" id="company-content" name="description" rows="5" spellcheck="true" class="form-control autosize-target text-left">
                                {{$company->description}}</textarea>
      </div>

      <div class="form-group">
        <input type="submit" class="btn btn-primary" value="Submit" />

      </div>

    </form>
  </div>

</div>

Replacing post with get,put removes the error but not doing what I want.

These are my routes

<?php


Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('companies','CompaniesController');
Route::resource('projects','ProjectsController');
Route::resource('roles','RolesController');
Route::resource('tasks','TasksController');
Route::resource('users','UsersController');

In the CompaniesController I have

 public function update(Request $request, Company $company)
    {

        $companyupdates = Company::where('id', $company->id)->update([
            'name' => $request->input('name'),
            'description' => $request->input('description'),
        ]);

        if($companyupdates){
            return redirect()->route('companies.show', ['company'=>$company->id])->with('success','Company Updated Successfully');
        }
        return back()->withInput();
    }

Where am I going wrong?

Upvotes: 3

Views: 2801

Answers (1)

Piazzi
Piazzi

Reputation: 2636

Try using the blade directives instead:

<form method="post" action="{{ route('companies.update',$company->id) }}">
      @csrf
      @method('PUT')

Note: you don't need to pass the company id with '[ ]'

In this input:

<input type="hidden" name="method" value="put">

The name should be _method according to the laravel form method spoofing

Example from the docs:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

With the blade directives:

<form action="/foo/bar" method="POST">
    @method('PUT')
    @csrf
</form>`

Why is this error occurring?

You put the wrong name on your method input, so laravel will recognize this form action as POST, and not PUT. Since it's a update action, laravel will thrown this error.

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:

For more info: Docs

Upvotes: 5

Related Questions