0

In my AngularJS Service I have an array of id's which I want to pass to a PHP server so it can delete them but somehow I keep on getting a 500/Internal Server Error response.

In my server log it's saying that the request is missing one argument which means the passing to the server wasn't successful.

I have something like this in my service:

destroySelected : function (ids) {
  // console.log(ids);
  // return $http.delete('/posts/destroySelected', ids);
  return $http({
    method: 'DELETE',
    url: '/posts/destroySelected/',
    headers: {'Content-Type': 'application/json;charset=utf-8'},
    data: {ids : ids}
  });
}

For my php controller I have this:

public function destroySelected($ids) {
    echo "<pre>" . var_export($ids, true) . "<pre>";
    die;
    return response()->json(Post::get());
}

my route:

Route::delete('posts/destroySelected/', 'PostController@destroySelected');

It's empty, but I wanted to double check that it's being passed successfully before I do anything else.

Can someone tell me what's going wrong here?

1
  • @jakubwrona that's the original route I tried using with what Georges provided, I added {post} to the url and still trying to work on it. Commented Oct 26, 2016 at 19:57

1 Answer 1

1

You didn't provided any data to the controller :)

You have : url: '/posts/destroySelected/' and data: {ids : ids}

So, your final url will be /posts/destroySelected/?ids=1 (for example)

The parameter $ids on your request require an url route parameter (for example route /posts/destroySelected/{ids}) and not a query parameter (_GET/_POST)

Solution:

        return $http({
            method: 'DELETE',
            url: '/posts/destroySelected/' + ids,
            headers: {'Content-Type': 'application/json;charset=utf-8'}
        });

To get the query parameter (_GET), you can use:

<?php
public function destroySelected(Request $request) {
    $ids = $request->input('ids'); // will get the value of $_REQUEST['ids'] ($_GET or $_POST)
    // var_dump($request->all()); for all values

    echo "<pre>" . var_export($ids, true) . "<pre>";
    die;
    return response()->json(Post::get());
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried few ways and this was one of them but I think I forgot about the Request $request in the first place thanks a lot

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.