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
I resolved it.
Route
Controller
Blade
You might have your routes cached, you can try running the following commands in the terminal:
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:
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.
How to Debug:
php artisan route::list --path admineditcountryController
php artisan route:clear
I would not recommend running
route:cache
as you’ll have to continuouslyroute:clear
any time you change/add/remove routes — set that up in your CI to only run in PROD. In fact — you should just runphp 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)
2. Reference controllers using an import
This is a bit cleaner (PHP 7+) + try utilizing
Route::resource
+ Laravel Action Verbs as controller function names docs3. Extra Credit
Try generating your controllers (benefits from PSR-2 compliant namespace & folder layouts):