B
B
BonBon Slick2016-10-30 13:06:34
JavaScript
BonBon Slick, 2016-10-30 13:06:34

Bulk Ajax deletion of Laravel 5.3 records?

My ajax request:

var users_ids = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']
    $.ajax({
      url: '/users/mass_delete',
      type: 'DELETE',
 		data: {
        "users_ids": users_ids,
        "_method": 'DELETE',
        "_token": token,
      },
    })
    .done(function( response ) {
 		console.log("Success!!!");
    })
    .fail(function() {
      console.log("Error!!!");
    })

My web.php ( routes ):
Route::delete('users/mass_delete', '[email protected]_delete');

And most importantly, the controller:
public function mass_delete( Request $users_ids ){
    $ids = $users_ids->all();
    $ids_to_delete = array_map(function($item){ return $item[0]; }, $ids);
    DB::table('users')->whereIn('user_id', $ids_to_delete)->delete(); 
  }

Deletes only the 1st entry from the array, that is, only the user with ID = 1 =/
How to remove all users with ID from the array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Perin, 2016-10-30
@BonBonSlick

try
User::find($users_ids->all())->delete();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question