Home > Enterprise >  The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel
The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel

Time:12-01

I am trying to update .

I am getting the error

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

Following are the code

In Route

Route::post('editcountries','App\Http\Controllers\backend\admineditcountryController@editcountries');

In Controller

 function editcountries(Request $request)
    {


    $country_en = $request->input('country_en');
    $country_ar = $request->input('country_ar');
    $id = $request->input('idd');
    $cstatus = 1;

    if (isset($request->status))
    $cstatus = 1;
    else
    $cstatus = 0;


    $isUpdateSuccess = country::where('id',$id)->update(['country_en'=> $country_en,
                                                                'country_ar'=> $country_ar,
                                                                'status' => $cstatus
                                                               ]);

    $this->clearToastr();
    session()->put('updated','Information updated Successfully.');
    return view('backend.adminaddcountry');

    }

In blade

 <form action="editcountries" method="POST" enctype="multipart/form-data">
                   
                    @csrf
                    {{-- @method('PUT') --}}
                    {{-- <input type="hidden" name="_method" value="PUT">" --}}

                  <div >
                    <label>Country Name in English </label>
                    <input type="text" name="country_en" placeholder="Country Name in English"
                    value='{{ $clist->country_en }}' required/>
                  </div>

                  <div >
                    <label>Country Name in Arabic</label>
                    <input type="text" dir="rtl" name="country_ar" placeholder="اسم الدولة بالعربية" value='{{ $clist->country_ar }}' />
                  </div>
                  <!-- end input -->

                  <div >
                    <input  name="status" type="checkbox" id="toggleSwitch2" @if($clist->status==1)
                    checked=""
                    @else
                    @endif>
                    <label  for="toggleSwitch2">Enable or disable the country.</label>
                  </div>

                  <input type="hidden" id="idd" name="idd" value="{{ $clist->id }}" />
                  <br>



                  <button >Update</button>

                  </form>

I tried using

@method('PUT') 

but then getting error

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

Please help

I tried putting

I tried using

@method('PUT') 

but then getting error

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

Please help

CodePudding user response:

You might have your routes cached, you can try running the following commands in the terminal:

php artisan route:clear
php artisan route:cache

You should get rid of the code you added making it a put request, unless that is what you want. If you want it to be a PUT route you need to use:

Route::put

Otherwise if you still can't figure it out, go through your routes file and check for any other routes that match "editcountries". Sometimes the order you put the routes can cause conflicts with other routes, like a resource route for example. However I would say if you get rid of the PUT code in the blade file, and run the route cache commands I provided, you should have it working with the post route.

CodePudding user response:

How to Debug:

  1. Run: php artisan route::list --path admineditcountryController
  2. If you do not see your controller in the output run php artisan route:clear

I would not recommend running route:cache as you'll have to continuously route:clear any time you change/add/remove routes — set that up in your CI to only run in PROD. In fact — you should just run php artisan optimize in production and it'll do a bunch of fancy performance Laravel magic for ya (further reading)

Other Improvements (PR Comments)

1. Follow PSR-2 File/Class/Namespace NAming

Aside, make sure your folder & namespaces are following PSR-2 (CamelCase)

App\Http\Controllers\backend\admineditcountryController
# should be
App\Http\Controllers\Backend\AdminEditCountryController
# even better (Namespaced) 
App\Http\Controllers\Backend\Admin\CountryController

2. Reference controllers using an import

This is a bit cleaner (PHP 7 ) try utilizing Route::resource Laravel Action Verbs as controller function names docs

use App\Http\Controllers\Backend\AdminEditCountryController;

/* Simplified - Best practice: use dashes as URL delimiters '-' */
Route::post('edit-countries', AdminEditCountryController::class . '@editcountries');

/* PRO VERSION */
Route::group('admin')->prefix('admin')->name('admin.')->group( function () {
  // AdminEditCountryController@update()
  Route::resource('country', Admin\CountryController::class)->only(['update']); 
});

3. Extra Credit

Try generating your controllers (benefits from PSR-2 compliant namespace & folder layouts):

php artisan make:controller Admin\CountryController --resource
  • Related