[英]I tried to delete data using ajax but not working
When I tried to delete id by using ajax, it is not working.当我尝试使用 ajax 删除 id 时,它不起作用。 I have tried many ways using the route name and also using route.我已经尝试了很多使用路由名称和路由的方法。 But it cannot work.但它不能工作。
My view page code is :我的查看页面代码是:
<a href="javascript:void(0)" id="delete-product" data-id="{{ $product->id }}" class="btn btn-danger delete-product">Delete</a>
My controller code is :我的控制器代码是:
function removedata(Request $request)
{
$product= Product::find($request->input('id'));
if($product->delete())
{
echo 'Data Deleted';
}
}
My route is :我的路线是:
Route::get('/product/remove', 'Product\ProductController@removedata')->name('product.remove');
My script is :我的脚本是:
$(document).ready(function () {
$('body').on('click', '.delete-product', function () {
var user_id = $(this).data("id");
if(confirm("Are you sure you want to Delete this data?"))
{
$.ajax({
url:"{{route('product.remove')}}",
mehtod:"get",
data:user_id,
success:function(data)
{
alert("sucess");
}
})
}
else
{
return false;
}
});
});
$product= Product::find($request->input('id'));
dd($product);--------------------------->(What is the output here)
if($product->delete())
{
echo 'Data Deleted';
}
Check this and update your code:检查并更新您的代码:
$product= Product::where('id',$request->input('id'))->delete();
if($product)
{
echo 'Data Deleted';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.