skip to Main Content

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


  1. Chosen as BEST ANSWER
    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.');
    }
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search