Reputation: 2043
I was working on a simple crud app using react + laravel. I am using Axios to make a request to the server. GET, POST is working fine. I am able to create a new item using POST method (axios.post). However, when I try to DELETE an item, laravel throws a 405 error, which stands for 'Method Not Allowed'
Here is my route :
Route::resource('items', 'ItemController');
Here is my destroy() method :
public function destroy($id)
{
$item = Item::find($id);
$item->delete();
return response()->json('Successfully Deleted');
}
Here is my delete function which uses axios :
export function deleteItem(data) {
let url = BASE_URL+'/items';
return axios.delete(url,data).then((response) => {
return response;
});
}
I did few research on this and found that laravel understands '_method' parameter too. So I trued the below :
export function deleteItem(data) {
let url = BASE_URL+'/items';
let payload = {'id' : data, '_method' : 'DELETE'};
return axios.post(url,payload).then((response) => {
return response;
});
}
But still no luck. It always throws 405. Any clue on this?
I am using axios & laravel at the latest version.
Upvotes: 1
Views: 2111
Reputation: 7420
The thing is that resource delete in laravel expects an id
to be sent via url.
public function destroy($id){
// ..find delete
}
In your case you are not sending the id, therfore laravel returns a notfoundhttpexception
So change the url to be smth along the lines: let url = BASE_URL_'/items/'+this.id;
Upvotes: 2