[英]Laravel getting collection delete nothing
I am sending collection of id's by checkbox
to my controller to delete them all together, I get network 200
but nothing will delete . 我by checkbox
将ID的集合发送到我的控制器以一起删除它们, 我得到了网络200
但是什么也不会删除 。
javascript
<script type="text/javascript">
$(document).ready(function() {
$('#multidel').on('click', function(e) {
e.preventDefault();
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
$.ajax({
url: '{{ url('admin/delmultipleproducts') }}',
type: "POST",
dataType: "json",
data: $('.productsfordel:checked').serialize(),
success:function(data) {
console.log('done');
}
});
$(document).ajaxStop(function(){
alert('successfully deleted.');
window.location.reload();
});
});
});
</script>
controller
public function multipledel(Request $request){
$product = Product::where('id', $request->productsfordel);
if(!empty($product->suboptions)){
$product->suboptions()->detach();
}
if(!empty($product->subspecifications)){
$product->subspecifications()->detach();
}
if(!empty($product->relatives)){
$product->relatives()->detach();
}
if(!empty($product->imageOne)){
Storage::delete($product->imageOne);
}
if(!empty($product->imageTwo)){
Storage::delete($product->imageTwo);
}
$product->delete();
// Session::flash('success', 'Selected products are successfully deleted.');
// return redirect()->route('products.index');
}
route
Route::post('delmultipleproducts', 'ProductController@multipledel')->name('delmultipleproducts');
any idea why is that? 知道为什么吗?
if i change my ajax data to serialize()
instead of val()
i get 如果我将ajax数据更改为serialize()
而不是val()
我会得到
array:3 [
0 => "35"
1 => "34"
2 => "33"
]
and when i delete then only id = 35
will delete, `only one of them instead of all of them. 当我删除时,只有id = 35
会删除,`只会删除其中一个而不是全部。
I think you are returning the collection here - 我想您要在这里归还收藏品-
$product = Product::where('id', $request->productsfordel);
You should actually return the first product, 您实际上应该退货第一个产品,
$product = Product::where('id', $request->productsfordel)->get()->first();
OR if you are sure it's indexed on a primary key (ie your id is a primary key 或者,如果您确定它已在主键上建立索引(即您的ID是主键)
You should do 你应该做
$product = Product::find($request->productsfordel);
OR if you want to delete all the products according to request 或者,如果您想根据要求删除所有产品
foreach($request->productsfordel as $id) {
$product = Product::find($id);
if(!empty($product->suboptions)){
$product->suboptions()->detach();
}
if(!empty($product->subspecifications)){
$product->subspecifications()->detach();
}
if(!empty($product->relatives)){
$product->relatives()->detach();
}
if(!empty($product->imageOne)){
Storage::delete($product->imageOne);
}
if(!empty($product->imageTwo)){
Storage::delete($product->imageTwo);
}
$product->delete();
You are getting an array of IDs. 您将获得一组ID。 So this would work. 这样就可以了。
$products = Product::whereIn('id', $request->productsfordel)->get();
foreach($products as $product){
$product->suboptions()->detach();
$product->subspecifications()->detach();
$product->relatives()->detach();
if(!empty($product->imageOne)){
Storage::delete($product->imageOne);
}
if(!empty($product->imageTwo)){
Storage::delete($product->imageTwo);
}
$product->delete();
}
Or you could do all this in Laravel observables and in here just write 或者您可以在Laravel Observables中完成所有这些操作,在这里只需编写
Product::whereIn('id', $request->productsfordel)->delete();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.