0

I am getting id's currently i an alert i want to post these id's to destroyAll method in my userController here is my method through which i am getting id's in alert

function deleteAll () {
    var checkedValues = $('input:checkbox:checked').map(function() {
        return this.value;
    }).get();
    alert(checkedValues);
}

i want to post these values through ajax and delete there route is as

Route::post('/user-management/user/destroyAll', 'UserController@destroyAll');

in destroyall method i also want to explode , and minus head checkbox value

2 Answers 2

2

You need to use ajax:

    $.ajax({
        type    : "POST",
        url     : "{{ url('/user-management/user/destroyAll') }}",
        data    : {ids: checkedValues, _token: "{{ csrf_token() }}"},
        success : function (response) {
            console.log('Success', response);
        },
        error   : function (response) {
            console.log('Error', response);
        },
        dataType: "JSON"
    });

and then catch ids in controller:

public function destroyAll(Request $request)
{
    // get ids
    $ids = $request->input('ids');

    // remove first
    array_shift($ids);

    // delete users with id
    return User::whereIn('id', $ids)->delete();
}
Sign up to request clarification or add additional context in comments.

3 Comments

what is array_shift($ids); used for?
It will remove first element of array, because OP asked "and minus head checkbox value" :)
@GiedriusKiršys please can you add whole method deleteAll clearly and update your answer please
0

Join your values to a string add them to a hidden input and in your controller you do a whereIn function to get all the users and delete them :

html:

<input type="hidden">

js:

$('input[type="hidden"]').val(checkedValues.join(','));

php:

public function destroyAll(Request $request)
        {
            $checkedValues = explode(','$request->input("checkedValues"));
            $user = User::whereIn('id',$checkedValues);
            $user->delete();
            //redirect back to the page after delete 
        }

refine your selector to not select the first one:

checkedValues = $('input:checkbox:checked:not(:first)')

5 Comments

how will i come to destroyAll method ? @madalin ivascu
you use a form with that hidden input filed in it
this js will be after alert(checkedValues); ? @madalin ivascu
just take it logically where will you put it? before the variable is initiated or after?
Don'y know sir that's why asking @madalin ivascu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.