405 (Method Not Allowed)

getting an error when i try to update .... PUT http://127.0.0.1:8000/api/task 405 (Method Not Allowed), can someone help?

public function update(Request $request, $id)
{
     $currentUser = JWTAuth::parseToken()->authenticate();

     $task = $currentUser->tasks()->find($id);

     if(!$task)
     throw new NotFoundHttpException;

      $task->fill($request->all());

      if($task->save())
            return $this->response->noContent();
      else
         return $this->response->error('could_not_update_task', 500);
}

Upvotes: 0

Views: 2455

Answers (3)

Thanks guys for the help, after hack and hack, I realized my Restangular.one("api/task").customPUT(data, taskId).then(function (response) FUNCTION was not receiving data, so PUT was hitting on the api route with no data causing the not allowed method error.

Upvotes: 1

Jasper Martin
Jasper Martin

Reputation: 180

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form. (Laravel Docs)

Can you use GET or POST method?

or

{!! Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>'col-md-12')) !!} .... wathever code here {!! Form::close() !!}

something like this. Hope this help

EDIT: I just saw your route and your controller. It expects a slug or unique identifier (which in case is id), so your route must look something like this

Route::put('/api/task/{id}', 'YourController@update');

This gives your controller the unique identifier you want.

Upvotes: 0

Mahdi Younesi
Mahdi Younesi

Reputation: 7499

The methodNotAllowed exception indicates that a route doesn't exist for the HTTP method you are requesting.

this route http://127.0.0.1:8000/api/task looks like a store route

Update will be like http://127.0.0.1:8000/api/task/1

so make sure you have added the route for method

Upvotes: 2

Related Questions