How can i delete users by chosing them with checkboxes

Published 1 month ago by uhbc

Here is how the table looks like:

http://prntscr.com/irpfe3

                                            <td><a href="javascript:;" onclick="if(confirm('Are you sure you wanna delete the user?'))location.href='{{route('deleteuser',array('id'=>$row->id))}}'">Delete it!</a></td>

Routes/web.php:

Route::get('/user/delete/{id}', '[email protected]')->name('deleteuser');

Here is, how the UserController looks like:



public function delete($id){
        if (\Auth::user()->role != 'admin')
        {
            return \Redirect::to('/home');
        }
        $status = \DB::delete('delete from users where id=?',array($id));
        if($status)
        {
            return \Redirect::to(route('allusers',array('info'=>'Account deleted')));
        }else{
            return \Redirect::to(route('allusers',array('error'=>'An error occured, try again later.')));
        }
    }


The question here is; When I click on the "Delete it!", it deletes the account. But the thing I want is, when I chose the checkboxes under 'Multiple tasks' and click on a button, remove the chosen ones from DB. How can I do that?

Best Answer (As Selected By uhbc)
Cronix

You don't have to manually add all of the ?'s. It does that automatically depending on how many id's are in the array if you are using Eloquent, which you aren't.

I'm not going to do it all, but here's how you'd basically do it

<form method="post" action="/route/to/your/delete">
    {{ csrf_field() }}
    <div>
        <input type="checkbox" name="ids[]" value="1">User 1
    </div>
    <div>
        <input type="checkbox" name="ids[]" value="10">User 10
    </div>
    <input type="submit">Send</input>
</form>

Notice how the name of the checkboxes are all the same, "ids[]", with brackets. That sends an array called "ids" to the controller that the form is posting to.

Then in the controller:

public function delete(Request $request)
{   
    // Get the ids array from the $request object
    // this will be the array. Don't use the [] here.
    $checkedIds = $request->ids; 

    //this will delete all ids that were checked, whether it was 1 or a million.
    User::whereIn('id', $checkedIds)->delete();  
}

This is using an Eloquent model, called User, which you should have by default. I wouldn't use DB:: manually to do this stuff or you would have to manually add in all of the ?'s as well.

As I said, this isn't complete. You'll have to adjust for your actual code.

wilk_randall

You need to be able to retrieve the ids of the users that are checked. Then send an array of those ids to your controller, and you will be able to run a query like this:

public function delete(Request  $request)
{
    User::whereIn('user_id', $request->ids)->delete();
}

Of course you may need to modify that query depending on your needs, it's just a really simple example.

uhbc
uhbc
1 month ago (1,260 XP)
 $status = \DB::delete('delete from users where id=?',array($id));
        if($status)

Isnt this the same thing as you said? Sorry, I am really new to this stuff.

Cronix
Cronix
1 month ago (334,240 XP)

No, your way deletes a single ID. The way @wilk_randall showed deletes many, using whereIn.

If it was deleting 2 ids, it would make: 'delete from users where id IN (?, ?)', [1, 2]

If it was deleting 3 ids, it would make: 'delete from users where id IN (?, ?, ?)', [1, 2, 6]

uhbc
uhbc
1 month ago (1,260 XP)

how can i adapt it to work, when i chose them with checkboxes? And will I have to add '?' as much as how many users i wanna delete up to. Like, if i wanna remove up to 50 times, will i have to add 50 ?s.

Cronix
Cronix
1 month ago (334,240 XP)

You don't have to manually add all of the ?'s. It does that automatically depending on how many id's are in the array if you are using Eloquent, which you aren't.

I'm not going to do it all, but here's how you'd basically do it

<form method="post" action="/route/to/your/delete">
    {{ csrf_field() }}
    <div>
        <input type="checkbox" name="ids[]" value="1">User 1
    </div>
    <div>
        <input type="checkbox" name="ids[]" value="10">User 10
    </div>
    <input type="submit">Send</input>
</form>

Notice how the name of the checkboxes are all the same, "ids[]", with brackets. That sends an array called "ids" to the controller that the form is posting to.

Then in the controller:

public function delete(Request $request)
{   
    // Get the ids array from the $request object
    // this will be the array. Don't use the [] here.
    $checkedIds = $request->ids; 

    //this will delete all ids that were checked, whether it was 1 or a million.
    User::whereIn('id', $checkedIds)->delete();  
}

This is using an Eloquent model, called User, which you should have by default. I wouldn't use DB:: manually to do this stuff or you would have to manually add in all of the ?'s as well.

As I said, this isn't complete. You'll have to adjust for your actual code.

Snapey
Snapey
1 month ago (924,495 XP)

make sure you use a form with POST method to delete users otherwise web crawlers can end up deleting users as they follow links.

In your list of users, put the user id in the value of your checkbox element

name the checkboxes with [] for instance name=users[] so that you end up with an array when the form is posted

uhbc
uhbc
1 month ago (1,260 XP)

Worked in localhost but can't set it to work on my website :/

shez1983

what error do you get?

@cronix lol i am not going to do it all... looks at your code.. pretty sure YOU DID ALL...

Cronix
Cronix
1 month ago (334,240 XP)

@shez1983 hah, I just meant I wasn't going to make it work with his js and stuff. Just a simple straight-up example.

uhbc
uhbc
1 month ago (1,260 XP)

Worked on my website too but, there are 2 problems now.

1- Admin panel is looking like http://prntscr.com/irswyj 2- And when I click on the 'Do it', It just deletes the account in the same line with 'Do it'.

uhbc
uhbc
1 month ago (1,260 XP)

the checkbox :


<input type="checkbox" name="ids[]" value="{{$row->id}}">

Cronix
Cronix
1 month ago (334,240 XP)

What is the code for that "do it" button (including javascript)?

I'd get rid of those "do it" buttons. You'd have just one submit at the bottom of your form, like in my example. No point in having a button next to each row if you want to select multiple checkboxes.

uhbc
uhbc
1 month ago (1,260 XP)

It was for the old 'Delete it' But like i said, it only deletes the acc in the same link with it. Thats why i wanna use checkboxes

Cronix
Cronix
1 month ago (334,240 XP)

So get rid of them and have a single "delete" submit button at the bottom.

shez1983

seems you need basic understanding of HTML.. of course that button will delete one its just submitting ONE value..

like mentioned you need to do what cronix suggested

Please sign in or create an account to participate in this conversation.