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

anderking's avatar

How can I delete records with the checkbox?

I have a user table and I want to display in each row a check box where the admin user can select the number of users he wants and press a delete button that deletes all selected users.

I was able to do a function that eliminates all the users at the same time.

public function destroyall()
{
    $user = User::all();
    $user->each(function($user){
        if ($user->type=="member")
            $user->delete();
    });
    if( $user->where('type','member')->count() )
        Flash('All member type users have been deleted','danger');
    else
        Flash('There are no users of member type','info');
    return redirect()->route('admin.users.index');
}

I know I can create a user [] of type checkbox, but how do I get the array in this function and loop where only users with the selected ID are deleted?

0 likes
1 reply
ottoszika's avatar
Level 7

Put on each row a checkbox with the name "users[]" and the user ID as value. On submitting the form, just take the array and prepare a whereIn query builder and call delete method on it (Hoping you will select only a few number of users, because the generated query will be as long as the selection array, otherwise just loop over each User instance).

public function destroyAll(Request $request)
{
    $selectedIds = $request->input('users');
    User::whereIn('id', $selectedIds)->delete();
}
1 like

Please or to participate in this conversation.