laravel 我的删除路径是重定向到show.blade,而不是销毁相应id [closed]的数据

nuypyhwy  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(101)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

22小时前关闭
Improve this question
我试图删除一个特定的数据与相应的id,但它重定向我到显示页面,虽然路由是指导它销毁。
我的索引刀片

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

我的毁灭控制器

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

我的控制器用于显示

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

我的索引控制器

{
        $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.
bvjxkvbb

bvjxkvbb1#

您的删除按钮窗体没有按钮type="submit"也没有method="post"尝试下面的代码。

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

我希望这能解决你的问题。

2jcobegt

2jcobegt2#

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!!!');
}

相关问题