recovery men recovery men - 26 days ago 15
Ajax Question

Delete data on base of selected checkbox ids laravel 5.3

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

Answer

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();
}
Comments