英文:
I using PUT method but keep showing not allowed
问题
Controller
:
public function edit(Product $product)
{
return view('products_update', compact('product'));
}
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'name' => 'required',
'desc' => 'required',
'price' => 'required|numeric',
'qty' => 'required',
// Add more validation rules for other fields if needed
]);
// Update the product
$newTitle = $request->input('product_title');
$newDesc = $request->input('product_desc');
$newPrice = $request->input('product_price');
$newQty = $request->input('product_qty');
// Update other fields if needed
//$product->save();
DB::update('update products set title = ?, desc = ?, price = ?, qty = ? where id = ?',
[$newTitle, $newDesc, $newPrice, $newQty, $id]);
// Redirect to the index page or show a success message
return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}
blade
:
<form action="{{ route('products.edit', $product->id) }}" method="post">
@method('PUT')
{{-- <input type="hidden" id="_token" value="{{ csrf_token() }}"> --}}
@csrf
</form>
route
:
Route::get('/products/{product}/edit', [ProductController::class, 'edit'])->name('products.edit');
Route::put('/products/{id}', [ProductController::class, 'update'])->name('products.update');
英文:
Controller
public function edit(Product $product)
{
return view('products_update', compact('product'));
}
public function edit(Product $product) { return view('products_update', compact('product')); }
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'name' => 'required',
'desc' => 'required',
'price' => 'required|numeric',
'qty' => 'required',
// Add more validation rules for other fields if needed
]);
// Update the product
$newTitle = $request->input('product_title');
$newDesc= $request->input('product_desc');
$newPrice= $request->input('product_price');
$newQty= $request->input('product_qty');
// Update other fields if needed
//$product->save();
DB::update('update products set title = ?, desc = ?, price = ?, qty = ? where id = ?',
[$newTitle, $newDesc, $newPrice, $newQty]);
// Redirect to the index page or show a success message
return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}
blade
<form action="{{ route('products.edit', $product->id) }}" method="post">
@method('PUT')
<!-- <input type="hidden" id="_token" value="{{ csrf_token() }}"> -->
@csrf
route
Route::get('/products/{product}/edit', \[ProductController::class,'edit'\])-\>name('products.edit');
Route::put('/products/{id}', \[ProductController::class, 'update'\])-\>name('products.update');
答案1
得分: 1
首先,你必须检查你的路由名称。当前你正在使用 <form action="{{ route('products.edit', $product->id) }}" method="post">
,但实际上你的路由名称应该是 products.update
用于更新数据。
根据你的路由 Route::put('/products/{id}', [ProductController::class, 'update'])->name('products.update');
所以,你必须将你当前的表单操作
从 action="{{ route('products.edit', $product->id) }}"
改为 action="{{ route('products.update', $product->id) }}"
。
最后一件事,如果更新按钮无法正常工作,它就像你之前说的那样停留在页面上,确保你的 <form></form>
内部有 <button type="submit">
来提交你的更新数据。
英文:
first of all you must check on your route name. currently you're using <form action="{{ route('products.edit', $product->id) }}" method="post">
. but actually your route name is products.update
for update data.
Based on your route Route::put('/products/{id}', \[ProductController::class, 'update'\])-\>name('products.update');
So, you must change your currently action form
from action="{{ route('products.edit', $product->id) }}"
into action="{{ route('products.update', $product->id) }}"
.
And the last things, if the update button cannot be function. It just stick on the page like you said before, make sure you have <button type="submit">
inside your <form></form>
to submit your update data
答案2
得分: 0
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'product_title' => 'required',
'product_desc' => 'required',
'product_price' => 'required|numeric',
'product_qty' => 'required',
// Add more validation rules for other fields if needed
]);
// Retrieve the product from the database
$product = Product::findOrFail($id);
// Update the product attributes with the validated data
$product->title = $request->product_title;
$product->desc = $request->product_desc;
$product->price = $request->product_price;
$product->qty = $request->product_qty;
// Update other fields if needed
$product->save();
// Redirect to the index page or show a success message
return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}
英文:
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'product_title' => 'required',
'product_desc' => 'required',
'product_price' => 'required|numeric',
'product_qty' => 'required',
// Add more validation rules for other fields if needed
]);
// Retrieve the product from the database
$product = Product::findOrFail($id);
// Update the product attributes with the validated data
$product->title = $request->product_title;
$product->desc = $request->product_desc;
$product->price = $request->product_price;
$product->qty = $request->product_qty;
// Update other fields if needed
$product->save();
// Redirect to the index page or show a success message
return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论