我的删除路由将我重定向到show.blade,而不是销毁具有相应ID的数据。

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

My route to delete is redirecting me to show.blade instead of destroying the data with corresponding id

问题

My index.blade

<td><form class="action"><button type="submit" formaction="{{route('products.show',$product->id)}}">查看</button></form></td>

<td><form class="action"><button type="submit" formaction="{{route('products.edit',$product->id)}}">编辑</button></form></td>

<td><form class="delete" action="{{ route('products.destroy',$product->id) }}">
  @csrf
  @method('DELETE')
  <button>删除</button>
  </form></td>

My Controller for destroy

public function destroy(Product $product)
{
    $product->delete();
    return redirect()->route('products.index')->with('success','数据删除成功!!!');
}

My Controller for show

public function show(Product $product)
{
    return view('products.show',compact('product'));
}

My controller for index

public function index()
{
    $products = Product::latest()->paginate(5);
    return view('products.index',compact('products'));
}

路由

Route::resource('/products',ProductController::class);

我也尝试使用了<a>链接标签,但结果相同,当我手动从数据库中删除数据时,数据也被成功删除。

英文:

I am trying to delete a particular data with corresponding id but it redirects me to the Show page although the route is directing it to destroy.

My index.blade

&lt;td&gt;&lt;form class=&quot;action&quot;&gt;&lt;button type=&quot;submit&quot; formaction=&quot;{{route(&#39;products.show&#39;,$product-&gt;id)}}&quot;&gt;Show&lt;/button&gt;&lt;/form&gt;&lt;/td&gt;

&lt;td&gt;&lt;form class=&quot;action&quot;&gt;&lt;button type=&quot;submit&quot;  formaction=&quot;{{route(&#39;products.edit&#39;,$product-&gt;id)}}&quot;&gt;Edit&lt;/button&gt;&lt;/form&gt;&lt;/td&gt;

&lt;td&gt;&lt;form class=&quot;delete&quot; action=&quot;{{ route(&#39;products.destroy&#39;,$product-&gt;id) }}&quot;&gt;
  @csrf
  @method(&#39;DELETE&#39;)
  &lt;button&gt;Delete&lt;/button&gt;
  &lt;/form&gt;&lt;/td&gt;

My Controller for destroy

public function destroy(Product $product)
    {
        $product-&gt;delete();
        return redirect()-&gt;route(&#39;products.index&#39;)-&gt;with(&#39;success&#39;,&#39;Data deleted successfully!!!&#39;);
    }

My Controller for show

public function show(Product $product)
    {
        return view(&#39;products.show&#39;,compact(&#39;product&#39;));
    }

My controller for index

    {
        $products = Product::latest()-&gt;paginate(5);
        return view(&#39;products.index&#39;,compact(&#39;products&#39;));
    }````

Route
````Route::resource(&#39;/products&#39;,ProductController::class);````


I tried using &lt;a&gt; link tag also but it does the same and also the data gets deleted perfectly fine when I delete it from database manually.

</details>


# 答案1
**得分**: 3

你的删除按钮表单没有 `type="submit"` 按钮,也没有 `method="post"`。请尝试以下代码:

```html
<form class="delete" action="{{ route('products.destroy',$product->id) }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit">删除</button>
</form>
```

希望这能解决你的问题。

<details>
<summary>英文:</summary>

Your delete button form does not have a button `type=&quot;submit&quot;` also it doesn&#39;t have  `method=&quot;post&quot;` try below code.

    &lt;form class=&quot;delete&quot; action=&quot;{{ route(&#39;products.destroy&#39;,$product-&gt;id) }}&quot; 
    method=&quot;POST&quot;&gt;
        @csrf
        @method(&#39;DELETE&#39;)
        &lt;button type=&quot;submit&quot;&gt;Delete&lt;/button&gt;
    &lt;/form&gt;
    
I hope this will solve your issue.




</details>



# 答案2
**得分**: 0

index.blade 
```html
<form class="delete" action="{{ route('products.destroy',$product->id) }}" method="POST">
   @csrf
   @method('DELETE')
   <button type="submit">Delete</button>
</form>
```

controller
```php
public function destroy(string $id)
{
    $product= Product::findOrFail($id);
    $product->delete();
    return redirect()->route('products.index')->with('success','Data deleted successfully!!!');
}
```

<details>
<summary>英文:</summary>

    index.blade 
    &lt;form class=&quot;delete&quot; action=&quot;{{ route(&#39;products.destroy&#39;,$product-&gt;id) }}&quot; 
    method=&quot;POST&quot;&gt;
       @csrf
       @method(&#39;DELETE&#39;)
       &lt;button type=&quot;submit&quot;&gt;Delete&lt;/button&gt;
    &lt;/form&gt;

    controller

    public function destroy(string $id)
    {
        $product= Product::findOrFail($id);
        $product-&gt;delete();
        return redirect()-&gt;route(&#39;products.index&#39;)-&gt;with(&#39;success&#39;,&#39;Data 
        deleted successfully!!!&#39;);
    }

</details>



huangapple
  • 本文由 发表于 2023年6月29日 18:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580341.html
匿名

发表评论

匿名网友

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

确定