John Davidson

php - Wrong Method for Laravel Route, change the method in form

0 comments
Message:


I have a problem with Laravel 8 and AdminLTE. Basically, I installed Admin LTE and an object Editos. I have two buttons in this menu : ADD edito and EDIT. Both redirect to the same form, but with a different Laravel Route. So my form.blade.php starts with :


<form 
method="post"
action="{{ Route::currentRouteName() === 'editos.edit' ? route('editos.update', $edito->id) : route('editos.store') }}">

@if(Route::currentRouteName() === 'editos.edit')
@method('PUT')
@endif

@csrf

Both buttons redirects to correct routes. The ADD function works perfectly : I fill the form and press submit button, the 'editos.store function with POST method is called and the edito is stored in my database.
But when i click on EDIT, it goes with the edit route (ok), it loads the actual fields (ok), but i click on submit, it calls the 'editos.update' route (ok) BUT i got this Laravel error message :


The GET method is not supported for this route. Supported methods: PUT.
http://hibooexpert.home/admin/editos/update/11?_method=PUT&_token=wGibILt5dHCfIOWDMNIuiTYXNL4quDagNJAob4t2&body=%3Cp%3EJe%20suis%26nbsp%3B%20un%20texte%20exempleqsfdqdfsqf%3C%2Fp%3E%0D%0A&categories%5B0%5D=2&excerpt=sqdfsqdf&image=image00004.jpeg&meta_description=General&meta_keywords=keyyy&seo_title=Bonjour&slug=bonjourfsdf&tags=&title=Bonjour


My routes are defined as such in my web.php :


Route::prefix('admin')->group(function () {
Route::middleware('admin')->group(function () {
Route::put('editos/update/{id}', [BackEditoController::class, 'update'])->name('editos.update');
Route::name('admin')->get('/', [AdminController::class, 'index']);
Route::name('purge')->put('purge/{model}', [AdminController::class, 'purge']);
Route::name('editos.create')->get('edito/create', [BackEditoController::class, 'create']);
Route::name('editos.store')->post('editos/store', [BackEditoController::class, 'store']);
Route::name('editos.edit')->get('editos/edit/{id}', [BackEditoController::class, 'edit']);
Route::name('editos.destroy')->post('editos/destroy', [BackEditoController::class, 'destroy']);

Route::name('editos')->get('editos', [BackEditoController::class, 'index']);
});

});

( I just put the editos/update/ on top to try, and changed also the syntax, but putting it somewhere else doesn't work).
As you see, it's a PUT function, and it is also defined as a PUT function when I do route:list. I already tried dump-autoload and cache clear composer command.


The update function in the Controller is :


  public function update(EditoRequest $request, EditoRepository $repository, Edito $edito, $id = null)
{
$edito = null;
if($id) {
$edito = Edito::findOrFail($id);
}
$repository->update($edito, $request);
return back()->with('ok', __('The edito has been successfully updated'));
}

And the repository function is :


public function update($edito, $request)
{
$request->merge([
'active' => $request->has('active'),
'image' => basename($request->image),
]);
$edito->update($request->all());
$this->saveCategoriesAndTags($edito, $request);
}

I'm completly stuck, if anyone see the solution, that would be awesome !



My Answer:
The issue seems to be with the form method in your blade file. When you click on the EDIT button, it correctly redirects to the 'editos.edit' route, but when you submit the form, it is still using the POST method instead of the PUT method required for the 'editos.update' route.

To fix this issue, you need to change the method in the form based on the current route. You can update your form.blade.php file as follows:

php
method="{{ Route::currentRouteName() === 'editos.edit' ? 'put' : 'post' }}"
action="{{ Route::currentRouteName() === 'editos.edit' ? route('editos.update', $edito->id) : route('editos.store') }}">

@if(Route::currentRouteName() === 'editos.edit')
@method('PUT')
@endif

@csrf


By changing the method attribute in the form tag dynamically based on the current route, it should now correctly submit the form with the PUT method when editing an existing record. This should resolve the issue and allow the 'editos.update' route to be called with the correct method.

Rate this post

3 of 5 based on 8883 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us