产品未删除

时间:2019-01-22 05:11:57

标签: laravel eloquent laravel-5.7

他正试图从管理站点删除产品(即将从数据库中删除),但它显示错误页面,其中显示“找不到对象!”任何解决方案,

这是ProductsController的代码:

  public function deleteProduct($id = null){
    Product::where(['id'=>$id])->delete();
    return redirect()->back()->with('flash_message_success', 'Product has been deleted successfully');
}

view_products.blade.php:

  <a id="delProduct" rel="{{ $product->id }}" rel1="delete-product" href="javascript:" class="btn btn-danger btn-mini deleteRecord">Delete</a>

3 个答案:

答案 0 :(得分:0)

您可以尝试这种方法吗,尽管它有所不同,但是我喜欢将其特别用于javascript或ajax调用

    public function deleteProduct(Request $request){
    Product::find($request->id);
    return redirect()->back()->with('flash_message_success', 'Product has been deleted successfully');
}


<a id="delProduct" rel="{{ $product->id }}" rel1="delete-product" href="javascript:" class="btn btn-danger btn-mini deleteRecord">Delete</a>

我的jquery代码看起来像这样

$(document).ready(function(event){
event.preventDefault();
$('#delProduct').click(function(){
$id=$(this).attr('rel');
...then ajax call to the server here with the id

});     });

答案 1 :(得分:0)

尝试:

Product::find($id)->delete()

答案 2 :(得分:0)

这个怎么样?您可能不需要执行$ id = null。

public function deleteProduct($id){
    $product=Product::find($id);
    $product->delete();
    return redirect()->back()->with('flash_message_success', 'Product has been deleted successfully');
}