英文:
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
<td><form class="action"><button type="submit" formaction="{{route('products.show',$product->id)}}">Show</button></form></td>
<td><form class="action"><button type="submit" formaction="{{route('products.edit',$product->id)}}">Edit</button></form></td>
<td><form class="delete" action="{{ route('products.destroy',$product->id) }}">
@csrf
@method('DELETE')
<button>Delete</button>
</form></td>
My Controller for destroy
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')->with('success','Data deleted successfully!!!');
}
My Controller for show
public function show(Product $product)
{
return view('products.show',compact('product'));
}
My controller for index
{
$products = Product::latest()->paginate(5);
return view('products.index',compact('products'));
}````
Route
````Route::resource('/products',ProductController::class);````
I tried using <a> 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="submit"` also it doesn't have `method="post"` try below code.
<form class="delete" action="{{ route('products.destroy',$product->id) }}"
method="POST">
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
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
<form class="delete" action="{{ route('products.destroy',$product->id) }}"
method="POST">
@csrf
@method('DELETE')
<button type="submit">Delete</button>
</form>
controller
public function destroy(string $id)
{
$product= Product::findOrFail($id);
$product->delete();
return redirect()->route('products.index')->with('success','Data
deleted successfully!!!');
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论