Oscar Gallo
Oscar Gallo

Reputation: 253

Laravel 4.2 MethodNotAllowedHttpException on delete

i'm new with Laravel, so have a project, a simple CRUD, but the delete method is not working when i try to delete the data, and i don't really know why. This is the error:

Error:

throw new MethodNotAllowedHttpException($others);

Controller:

public function destroy($id)
{
    $project = Project::find($id);
    if($project->user_id==Auth::id()) {
        $project->delete();
        return Redirect::to('/');
    } else {
        Session::flash('message', 'You can't delete this!');
        return Redirect::to('/');
    }
}

View:

{{Form::open(array('url' => 'project/destroy/'.$p->id, 'method' => 'DELETE'))}}
    {{Form::submit("Delete", array('class' => 't2tButton text-center'))}}
{{Form::close()}}

Routes:

Route::post('/project/destroy/{id}', "ProjectController@destroy");

Upvotes: 3

Views: 2408

Answers (2)

Oscar Gallo
Oscar Gallo

Reputation: 253

I just figured out the answer, the error was on this line in Routes:

Route::delete('/project/destroy/{id}', "ProjectController@destroy");

the Route method needs to be DELETE

Upvotes: 0

Dan
Dan

Reputation: 9488

You have a route set for POST but not for DELETE.

Try adding this to your routes:

Route::delete('/project/destroy/{id}', "ProjectController@destroy");

Or you could change your method to POST and keep your route as is, but to keep it RESTful it's probably best to change to DELETE.

Upvotes: 5

Related Questions