B
B
BonBon Slick2016-10-30 14:57:56
JavaScript
BonBon Slick, 2016-10-30 14:57:56

How to pass an array of IDs to delete using Ajax to a Laravel 5 controller?

I try like this:

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,
      },
    })

And accept it
public function mass_delete( array $users_ids ){
if ( is_array( $users_ids ) && !empty( $users_ids ) ) {
      User::destroy( $users_ids );
    } else {
      User::findOrFail( $users_ids )->delete();
    }
   }

Gives an error message:
ErrorException in User_Controller.php line 31:
Argument 1 passed to App\Http\Controllers\User_controller::mass_delete() must be of the type array, none given
in User_Controller.php line 31
at HandleExceptions->handleError('4096', 'Argument 1 passed to App\Http\Controllers\User_controller::mass_delete() must be of the type array, none given', 'C:\Users\BonBonS\Desktop\OpenServer\domains\lara53\app\Http\Controllers\User_Controller.php', '31', array()) in User_Controller.php line 31
at User_controller->mass_delete()

If change
public function mass_delete( array $users_ids ){
//на
  public function mass_delete( Request $users_ids ){

Gives an error message:
NotFoundHttpException in Handler.php line 113:
No query results for model [App\User] DELETE /users/mass_delete HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,ru;q=0.6
Connection: keep-alive
Content-Length: 262
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: XSRF-...
in Handler.php line 113
at Handler->prepareException(object(ModelNotFoundException)) in Handler.php line 130
at Handler->render(object(Request), object(ModelNotFoundException)) in Handler.php line 47...
...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question