Dmitry Malys
Dmitry Malys

Reputation: 1323

on DELETE MethodNotAllowedHttpException in RouteCollection.php line 251:

Hello i try to delete my comments in Laravel 5.4, but unfortunately i get this error. Can't really get where is the problem.

This is my form:

<form action="{{route('comments.destroy', $comment->id) }}">
    <div class="form-group">
        <button type="submit" class="btn btn-danger">DELETE</button>
    </div>
    {{ method_field('DELETE') }}
</form>

This is my routes:

// Comments
Route::post('/lots/{lot}/comments', 'CommentsController@store');
Route::get('/comments', 'CommentsController@show');
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
Route::put('comments/{id}', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Route::delete('comments/{id}', ['uses' => 'CommentsController@destroy', 'as' => 'comments.destroy']);
Route::get('comments/{id}/delete', ['uses' => 'CommentsController@delete', 'as' => 'comments.delete']);

This is the controller:

public function delete($id)
    {
        $comment = Comment::find($id);
        return view('comments.delete')->withComment($comment);
    }

public function destroy($id)
    {
        $comment = Comment::find($id);
        $comment->delete();

        return back();
    }

Any ideas what i'm doing wrong?

This is the error message: enter image description here

Upvotes: 1

Views: 5336

Answers (3)

davefrassoni
davefrassoni

Reputation: 312

In my case my error was that I was not setting the id property on the object sent to the view, and that was why the form action URL didn't have it.

Check that you are setting id property in your comment object, so the URL is parsed as: .../comments/####

e.g.

$photos = Photo::all()->select('id', 'name')->get(); return view('photos.index')->with('photos', $photos);

Upvotes: 0

Parth Vora
Parth Vora

Reputation: 4114

This should work:

<form action="{{ route('comments.destroy', ['id' => $comment->id]) }}" method="post">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <div class="form-group">
        <button type="submit" class="btn btn-danger">DELETE</button>
    </div>
</form>

Upvotes: 2

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

I think I found the problem, Its your form action's route syntax which is wrong. route helper

<form action="{{route('comments.destroy', ['id' => $comment->id]) }}">
    {{ csrf_field() }}
    <div class="form-group">
        <button type="submit" class="btn btn-danger">DELETE</button>
    </div>
    <input type="hidden" name="_method" value="DELETE">
</form>

Upvotes: 1

Related Questions