Php 此路由不支持PUT方法。支持的方法:GET、HEAD、POST。在拉维尔
我想做卖家可以编辑和更新的产品 我是ProductControllerPhp 此路由不支持PUT方法。支持的方法:GET、HEAD、POST。在拉维尔,php,laravel,Php,Laravel,我想做卖家可以编辑和更新的产品 我是ProductController public function edit($id) { $product = Product::find($id); return view('product.edit', compact('product')); } /** * Update the specified resource in storage. * * @param
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();
}
这是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>
当我点击提交按钮进行更新时
此路由不支持PUT方法。支持的方法:GET、HEAD、POST。出现
我怎么能修好它?请帮助您应该使用PUT-in-route
Route::put('','ProductController@update')->name('product.update');
而不是produc.update不包括产品->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>
{{csrf_field()}}
{{method_字段('put')}
[...]
提交
您需要在表单请求中传递id
参数,如下所示:
<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>
这是因为您的控制器方法期望在请求中传递一个id
,但实际上没有收到,因此出现了错误
我希望这有帮助 请再试一次
Route::put('edit/{id}','ProductController@update')->name('product.update');
及
{{csrf_field()}}
{{method_字段('put')}
[...]
提交
函数App\Http\Controllers\ProductController::update()的参数太少,传递了1个,正好传递了2个expected@user12149659只需将表单操作路径更改为,请更新您以前的答案,如果它不能解决问题,如果卖家想要删除产品,该怎么做{{csrf_field()}{{method_field('DELETE')}DELETE Route::DELETE('','ProductController@destroy')->name('product.destroy')//卖家删除产品我尝试了这个,但出现了错误
Route::put('edit/{id}','ProductController@update')->name('product.update');
Route::put('edit/{id}','ProductController@update')->name('product.update');
<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>