Search code examples
laravellaravel-7laravel-controllerlaravel-routelaravel-formrequest

Apps route redirecting the wrong URL


------Platform Laravel 7x. --------

I am stuck in a simple problem. I can't find the error. While I am updating a form, it redirects to a wrong URL which I don't want and data doesn't update.

Form action url:

 method="POST" action="{{'city/update/'. $editCity->id}}"

form image

Route:

Route::post('city/update/{id}','Admin\CityController@update');

web route

Function from controller:

public function update(Request $request, $id)
    {
        $editCity=City::find($id);
        $editCity->city_name=$request->city_name;
        $editCity->save();
        return redirect()->back();
    }

function from controller

When I click on update it goes to this URL and shows 404 error which I don't want: public/panel/city/edit/city/update/33

Help me to find out the problem where is the mistake I have done. I want to make it updated when I click on the update button and return back.


Solution

  • Use name route instead. So your code will look like :

    blade.php

    method="POST" action="{{ route('city.update',  $editCity->id) }}"
    

    web.php

    Route::post('city/update/{id}','Admin\CityController@update')->name('city.update');