我使用PUT方法,但一直显示不允许

问题描述 投票:0回答:2

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');
php ajax laravel
2个回答
0
投票

首先你必须检查你的路线名称。目前您正在使用

<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) }}"

.

最后,如果更新按钮无法使用。它只是像你之前说的那样粘在页面上,确保你的

<button type="submit">
里面有
<form></form>
来提交你的更新数据


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.');
}
© www.soinside.com 2019 - 2024. All rights reserved.