3

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?

7
  • 3
    Show you're routes file, please. Commented Apr 12, 2019 at 15:25
  • 1
    I dont' know if this is causing the issue, but i think your should change your method input to "_method" as described in the laravel form method spoofing, or use the blade directive @method('PUT') Commented Apr 12, 2019 at 15:35
  • ^ we need to see your routes. Commented Apr 12, 2019 at 15:36
  • 2
    @Kamal Route::resource() is shorthand for a bunch of routes connected to a ResourceController; those are the full routes. Commented Apr 12, 2019 at 15:50
  • 2
    For resources, update can use PUT or PATCH, not POST, so see the answer below. Here's the documentation: laravel.com/docs/5.8/controllers#resource-controllers Commented Apr 12, 2019 at 15:56

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.