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');
2
Answers
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 isproducts.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