Reputation: 91
i want to do seller can edit and update product
this is ProductController
public function edit($id)
{
$product = Product::find($id);
return view('product.edit', compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$product = Product::find($id);
$product-> title = $request-> title;
$product-> description = $request-> description;
$product-> price = $request-> price;
if($request->hasFile('image')){
$file = $request-> file('image');
$filename = time().'.'.$file-> getClientOriginalExtension();
$location = public_path('/images');
$file-> move($location, $filename);
$oldImage = $product->image;
\Storage::delete($oldImage);
$product-> image= $filename;
}
$product-> save();
return back();
}
this is edit.blade.php
<form action="{{route('product.update', $product->id)}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
{{method_field('put')}}
[...]
<button type="submit" class="btn btn-success">Submit</button>
this is web.php
Route::get('/index', 'ProductController@index');//seller view all product
Route::get('/create', 'ProductController@create'); //seller create new product
Route::post('','ProductController@store')->name('product.store'); //store in database
Route::get('/edit/{id}','ProductController@edit'); // seller edit post
Route::post('','ProductController@update')->name('product.update'); //seller update
when i click submit button for update The PUT method is not supported for this route. Supported methods: GET, HEAD, POST. show up
how i can fix it? PLEASE HELP
Upvotes: 2
Views: 36721
Reputation: 461
You need to pass the id
parameter in your form request like below:
<form action="{{ route('product.update', $product->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('put') }}
[...]
<button type="submit" class="btn btn-success">Submit</button>
And then modify your controller method to be as below:
Route::put('edit/{id}','ProductController@update')->name('product.update');
This is because your controller method is expecting an id
to be passed through in the request but isn't actually receiving one, hence the error.
Upvotes: 1
Reputation: 1
if you have nothing wrong with routes or view files such as forgot to put @method('PUT') then once try running "php artisan optimize" command, it helped me it clears all config and route cache.
Upvotes: 0
Reputation: 444
Try again please;
Route::put('edit/{id}','ProductController@update')->name('product.update');
and
<form action="{{ route('product.update', ["id" => $product->id]) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('put') }}
[...]
<button type="submit" class="btn btn-success">Submit</button>
Upvotes: 5
Reputation: 444
You should use PUT in route;
Route::put('','ProductController@update')->name('product.update');
And not produc.update not except product->id
<form action="{{route('product.update')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
{{method_field('put')}}
[...]
<button type="submit" class="btn btn-success">Submit</button>
Upvotes: 1