2

I've a route defined as below:

Route::resource('api/invoice', 'InvoiceController');

In my controller I have a function destroy($id)

When sending a DELETE request to /api/invoice with an id: 2 as parameter and I get a 405 error message back. I tried accessing this route from Postman and from a javascript code directly, without success...

Any ideas?

chris
  • 36,115
  • 52
  • 143
  • 252
LEM01
  • 871
  • 3
  • 15
  • 23
  • Can you show your javascript code that you are calling the route from? – Jared Eitnier Oct 06 '14 at 23:12
  • It is an angularjs http call: $http.delete(this.config.BASE_API_URL+'/invoice', {'id': '2'}).success(function(data) { if (data.code = 1) { console.log("success"); } else { console.log("validation error"); } }); – LEM01 Oct 07 '14 at 16:57

2 Answers2

2

You said I get a 405 error and that is because of wrong HTTP Method, in your Resource Controller you have the destroy method and in this case this method is accessible using a DELETE request. So make sure you are sending a DELETE request for this.

You may run the following command from your terminal/command prompt:

php artisan routes

It'll output the routes with their names and URL so find the route and check the request method and URL to access that method. Check HTTP Error 405 Method not allowed for more information. If you are using JavaScript/AJAX to invoke the destroy method then, check this answer.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • php artisan routes, returns the list of rout where I have: DELETE api/invoice/{invoice} as expected. As I said, I tried both using js and postman chrome ext which creates a HTTP DELETE request. – LEM01 Oct 07 '14 at 16:58
  • 1
    Ok found the issue... really stupid, my url was incorrect: /api/invoice?id=1 instead of /api/invoice/1. Never work after midnight after a full workday ;-) Thanks for your help! – LEM01 Oct 07 '14 at 17:01
  • @LEM01, You are welcome and btw, I work at midnight, 12am - 6am is the best time for me :-) – The Alpha Oct 08 '14 at 01:05
0

405 error was returned because the request URL wasn't matching the DELETE URI:

Expected destroy URI: DELETE api/invoice/{invoice} Used destroy URI: DELETE api/invoice?id=1

As simple as that...

LEM01
  • 871
  • 3
  • 15
  • 23