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.
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?
UPDATE
if i change my ajax data to serialize()
instead of val()
i get
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.