skip to Main Content

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','AppHttpControllersbackendadmineditcountryController@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 class="input-style-1">
                    <label>Country Name in English </label>
                    <input type="text" name="country_en" placeholder="Country Name in English"
                    value='{{ $clist->country_en }}' required/>
                  </div>

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

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

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



                  <button class="main-btn primary-btn btn-hover text-center">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

3

Answers


  1. Chosen as BEST ANSWER

    I resolved it.

    Route

    Route::post('/editcountries','AppHttpControllersbackendadmineditcountryController@editcountries');
    

    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.');
    
        $data = country::where('id', '=',  $id)->first();
        return view('backend.admineditcountry',['clist'=>$data]);
    
        }
    

    Blade

       <form action="/editcountries" method="POST" enctype="multipart/form-data">
    
                        @csrf
    
                      <div class="input-style-1">
                        <label>Country Name in English </label>
                        <input type="text" name="country_en" placeholder="Country Name in English"
                        value='{{ $clist->country_en }}' required/>
                      </div>
    
                      <div class="input-style-1">
                        <label>Country Name in Arabic</label>
                        <input type="text" dir="rtl" name="country_ar" placeholder="اسم الدولة بالعربية" value='{{ $clist->country_ar }}' />
                      </div>
                      <!-- end input -->
    
                      <div class="form-check form-switch toggle-switch">
                        <input class="form-check-input" name="status" type="checkbox" id="toggleSwitch2" @if($clist->status==1)
                        checked=""
                        @else
                        @endif>
                        <label class="form-check-label" for="toggleSwitch2">Enable or disable the country.</label>
                      </div>
    
                      <input type="hidden" id="idd" name="idd" value="{{ $clist->id }}" />
                      <br>
    
    
    
                      <button class="main-btn primary-btn btn-hover text-center">Update</button>
    
                      </form>
    

  2. 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.

    Login or Signup to reply.
  3. 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)

    AppHttpControllersbackendadmineditcountryController
    # should be
    AppHttpControllersBackendAdminEditCountryController
    # even better (Namespaced) 
    AppHttpControllersBackendAdminCountryController
    

    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 AppHttpControllersBackendAdminEditCountryController;
    
    /* 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', AdminCountryController::class)->only(['update']); 
    });
    

    3. Extra Credit

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

    php artisan make:controller AdminCountryController --resource
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search