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

huangapple go评论65阅读模式
英文:

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(&#39;products_update&#39;, compact(&#39;product&#39;));
}

    public function edit(Product $product) { return view(&#39;products_update&#39;, compact(&#39;product&#39;)); }
    
    public function update(Request $request, $id)
    {
        $validatedData = $request-&gt;validate([
            &#39;name&#39; =&gt; &#39;required&#39;,
            &#39;desc&#39; =&gt; &#39;required&#39;,
            &#39;price&#39; =&gt; &#39;required|numeric&#39;,
            &#39;qty&#39; =&gt; &#39;required&#39;,
            // Add more validation rules for other fields if needed
        ]);
    
        // Update the product
        $newTitle = $request-&gt;input(&#39;product_title&#39;);
        $newDesc= $request-&gt;input(&#39;product_desc&#39;);
        $newPrice= $request-&gt;input(&#39;product_price&#39;);
        $newQty= $request-&gt;input(&#39;product_qty&#39;);
    
        // Update other fields if needed
        //$product-&gt;save();
    
        DB::update(&#39;update products set title = ?, desc = ?, price = ?, qty = ? where id = ?&#39;, 
        [$newTitle, $newDesc, $newPrice, $newQty]);
    
        // Redirect to the index page or show a success message
        return redirect()-&gt;route(&#39;products.index&#39;)-&gt;with(&#39;success&#39;, &#39;Product updated successfully.&#39;);
    }

blade

&lt;form action=&quot;{{ route(&#39;products.edit&#39;, $product-&gt;id) }}&quot; method=&quot;post&quot;&gt;
    @method(&#39;PUT&#39;)
    &lt;!-- &lt;input type=&quot;hidden&quot; id=&quot;_token&quot; value=&quot;{{ csrf_token() }}&quot;&gt; --&gt;
    @csrf

route

Route::get(&#39;/products/{product}/edit&#39;, \[ProductController::class,&#39;edit&#39;\])-\&gt;name(&#39;products.edit&#39;);
Route::put(&#39;/products/{id}&#39;, \[ProductController::class, &#39;update&#39;\])-\&gt;name(&#39;products.update&#39;);

答案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 &lt;form action=&quot;{{ route(&#39;products.edit&#39;, $product-&gt;id) }}&quot; method=&quot;post&quot;&gt;. but actually your route name is products.update for update data.

Based on your route Route::put(&#39;/products/{id}&#39;, \[ProductController::class, &#39;update&#39;\])-\&gt;name(&#39;products.update&#39;);

So, you must change your currently action form

from action=&quot;{{ route(&#39;products.edit&#39;, $product-&gt;id) }}&quot;

into action=&quot;{{ route(&#39;products.update&#39;, $product-&gt;id) }}&quot;

.

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 &lt;button type=&quot;submit&quot;&gt; inside your &lt;form&gt;&lt;/form&gt; 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-&gt;validate([
        &#39;product_title&#39; =&gt; &#39;required&#39;,
        &#39;product_desc&#39; =&gt; &#39;required&#39;,
        &#39;product_price&#39; =&gt; &#39;required|numeric&#39;,
        &#39;product_qty&#39; =&gt; &#39;required&#39;,
        // 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-&gt;title = $request-&gt;product_title;
    $product-&gt;desc = $request-&gt;product_desc;
    $product-&gt;price = $request-&gt;product_price;
    $product-&gt;qty = $request-&gt;product_qty;

    // Update other fields if needed
    $product-&gt;save();

    // Redirect to the index page or show a success message
    return redirect()-&gt;route(&#39;products.index&#39;)-&gt;with(&#39;success&#39;, &#39;Product updated successfully.&#39;);
}

huangapple
  • 本文由 发表于 2023年5月17日 14:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定