@lovercode Have you dumped $request yet? I think $request->ids should be replaced by $request->get('ids');
Besides that, because it's a DELETE sql command, I'ld highly recommend you to add additional validation.
i want delete many product but it have not error not working . where is error ?
Route::delete('myproductsDeleteAll', 'ProductController@deleteAll');
Controller
public function deleteAll(Request $request)
{
$ids = $request->ids;
DB::table("products")->whereIn('id',explode(",",$ids))->delete();
return response()->json(['success'=>"Products Deleted successfully."]);
}
route and view
<button
data-url="{{ url('myproductsDeleteAll') }}">
Delete All
</button>
@if($items->count())
@foreach($items as $key => $item)
<tr>
<td><input type="checkbox" class="sub_chk" data-id="{{$item->id}}"></td>
<td>{{ ++$key }}</td>
{{ $item->name }}
and
<script type="text/javascript">
$(document).ready(function () {
$('#master').on('click', function (e) {
if ($(this).is(':checked', true)) {
$(".sub_chk").prop('checked', true);
} else {
$(".sub_chk").prop('checked', false);
}
});
$('.delete_all').on('click', function (e) {
var allVals = [];
$(".sub_chk:checked").each(function () {
allVals.push($(this).attr('data-id'));
});
if (allVals.length <= 0) {
alert("Please select row.");
} else {
var check = confirm("Are you sure you want to delete this row?");
if (check == true) {
var join_selected_values = allVals.join(",");
$.ajax({
url: $(this).data('url'),
type: 'DELETE',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: 'ids=' + join_selected_values,
success: function (data) {
if (data['success']) {
$(".sub_chk:checked").each(function () {
$(this).parents("tr").remove();
});
alert(data['success']);
} else if (data['error']) {
alert(data['error']);
} else {
alert('Whoops Something went wrong!!');
}
},
error: function (data) {
alert(data.responseText);
}
});
$.each(allVals, function (index, value) {
$('table tr').filter("[data-row-id='" + value + "']").remove();
});
}
}
});
$('[data-toggle=confirmation]').confirmation({
rootSelector: '[data-toggle=confirmation]',
onConfirm: function (event, element) {
element.trigger('confirm');
}
});
$(document).on('confirm', function (e) {
var ele = e.target;
e.preventDefault();
$.ajax({
url: ele.href,
type: 'DELETE',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function (data) {
if (data['success']) {
$("#" + data['tr']).slideUp("slow");
alert(data['success']);
} else if (data['error']) {
alert(data['error']);
} else {
alert('Whoops Something went wrong!!');
}
},
error: function (data) {
alert(data.responseText);
}
});
return false;
});
});
</script>
Please or to participate in this conversation.