0

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

Community
  • 1
  • 1
Tusher
  • 23
  • 8

3 Answers3

0

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');
STA
  • 30,729
  • 8
  • 45
  • 59
  • Now it shows error for edit page, Error: Facade\Ignition\Exceptions\ViewException Missing required parameters for [Route: city.update] [URI: panel/city/update/{id}]. (View: D:\xampp\htdocs\tolethunt\resources\views\admin\editCity.blade.php) – Tusher May 19 '20 at 16:46
  • thanks, buddy. It works. Can you please tell me where I have done mistake without the name route? I want to learn. – Tusher May 19 '20 at 17:17
  • Your mistake seems like that : `method="POST" action="{{ url('city/update/'. $editCity->id) }}"` hope this also works – STA May 19 '20 at 17:22
  • You can also try this : `method="POST" action="/city/update/{{ $editCity->id }}"` hope you understand. Have a good day – STA May 19 '20 at 17:24
0

In case your CityController is a resource controller, this what you should try:

Route: web.php

Route::resource('city', 'Admin\CityController');   
Form: HTML

<form action="{{route('city.update',$editCity->id)}}" method="post">
Controller: CityController.php

public function update(Request $request, $id)
    {
        $editCity=City::find($id);
        $editCity->city_name=$request->city_name;
        $editCity->save();
        return back()->with('success','city added successfully!');
    }

I hope it helps !

  • The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE. this error occurred. I didn't use a resource controller. But after implementing your code I got that error. – Tusher May 19 '20 at 16:55
  • While creating your controller did you run such command ?: ``` php artisan make:controller Admin\CityController --resource ``` You should create the controller as a resource controller, then declare it in your routes like this: ``` Route::resource('city', 'Admin\CityController'); ``` Ps: Make sure to remove in web.php, your old route: ``` Route::post('city/update/{id}','Admin\CityController@update')->name('city.update'); ``` – Fanhatcha Kone May 19 '20 at 17:12
0

While creating your controller did you run such command ?:

php artisan make:controller Admin\CityController --resource

You should create the controller as a resource controller, then declare it

in your routes like this:

Route::resource('city', 'Admin\CityController');  

Ps: Make sure to remove in web.php, your old route:

Route::post('city/update/{id}','Admin\CityController@update')->name('city.update');
  • The problem is solved in another way. But I want to learn your way. I didn't create a resource controller. It only make:Admin\cityController So, if I create a resource controller, is the route will be same for edit, update, delete? and what should I have to pass in the delete and edit button href? – Tusher May 19 '20 at 17:15
  • Yeah. In case you want to perform intensive CRUD operations, it always good to declare your controller as a resource controller. Laravel will automatically create the HTTP verbs for you. – Fanhatcha Kone May 19 '20 at 17:21