Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zettz's avatar
Level 1

Delete multiple record return 500 Server Error

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",
                                });
                            }
                        })
                    }
                })
            );
        }
0 likes
7 replies
shakti's avatar

instead of using foreach($request['id'] as $id)

can you please use foreach($request->id as $id)

zettz's avatar
Level 1

@shakti I just changed it and try it, it give the same error

Snapey's avatar

you can pass the array to destroy() instead of looping.


User::destroy($request->id);

If this does not work you are going to have to investigate the specific error returned which you can do in the Laravel log file or by checking the response in your browser tools

zettz's avatar
Level 1

@Snapey Already tried that too, it worked, but the records not deleted from database.

Snapey's avatar

so check $request->id. chances are you are not sending the form data correctly

zettz's avatar
Level 1

@Snapey Is this line is the reason?

return Datatables::of($user)
->addColumn('check', function ($user) { return '<input class="selector" type="checkbox" name="id[]" value="' . $user->id . '">'; }) 
->make(true);

Because I can't find any other error at console log.

zettz's avatar
zettz
OP
Best Answer
Level 1

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.