instead of using foreach($request['id'] as $id)
can you please use foreach($request->id as $id)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I want to delete multiple record using checkbox, but it always return 500 (Internal Server Error). Here's the controller:
public function removeSelected(Request $request)
{
foreach($request['id'] as $id){
$user = tap(User::findOrFail($id));
$user->delete();
}
}
and here's the route:
Route::post('users/remove', 'UserController@removeSelected')->name('users.remove');
At the blade template, I use this button code (button outside form)
<button onclick="deleteAll()" class="btn btn-danger btn-sm"><i class="fa fa-times"></i> Remove Selected</button>
{{ Form::open(['method' => 'POST', 'id' => 'form-users']) }}
table content
{{ Form::close() }}
Everything works fine with no error result at blade view, error appear when I click at the button. The error message aside from 500 (Internal Server Error) is Invalid argument supplied for foreach().
Thank you.
Edit, here's the jquery code:
function deleteAll(){
if($('input:checked').length < 1){
swal({
title: 'Notice',
text: 'Please select user(s) to remove',
type: 'warning',
html: true,
confirmButtonColor: "#DD4B39",
confirmButtonText: "Confirm",
});
}else if
(
swal({
title: 'Warning',
text: 'Are you sure to remove the selected user(s)?',
type: 'warning',
html: true,
confirmButtonColor: "#DD4B39",
confirmButtonText: "Yes",
showCancelButton: true,
},
function(isConfirm) {
if (isConfirm) {
$.ajax({
url : "{{ route('users.remove') }}",
type : "POST",
data : $('#form-users').serialize(),
success : function(data){
swal({
title: 'Removed',
text: 'All selected post(s) have been removed.',
type: 'success',
html: true,
confirmButtonColor: "#DD4B39",
confirmButtonText: "Confirm"
}),
Pace.restart(),
$('table.display#tb_users').DataTable().ajax.reload();
},
error : function(){
swal({
title: 'Error',
text: 'Something went wrong.<br/>Can\t remove selected user(s).',
type: 'error',
html: true,
confirmButtonColor: "#DD4B39",
confirmButtonText: "Confirm",
});
}
})
}
})
);
}
I found the cause, its at this line, so I just need to remove it
"columnDefs":[
{ targets: 2, visible: false },
/* this line
{ targets: 0,
"checkboxes": {
"selectRow": true,
"selectCallback": function(nodes, selected){
$('input[type="checkbox"]', nodes).iCheck('update');
},
"selectAllCallback": function(nodes, selected, indeterminate){
$('input[type=checkbox]', nodes).iCheck('update');
}
}
}
*/
]
Please or to participate in this conversation.