heshammahmoud
2 years ago

Laravel 5.2 MethodNotAllowedHttpException

Posted 2 years ago by heshammahmoud

I got MethodNotAllowedHttpException error on my Laravel 5.2 project, while I was adding Add Comment section.

Here is my route:

Route::post('/posts/{post}/comments', '[email protected]');

Here is my CommentsController:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\Comment;
use App\Http\Requests;

class CommentsController extends Controller
{
    public function store(Post $post)
    {
        Comment::create([
            'body' => request('body'),
            'post_id' => $post->id
            ]);

        return back();
   }
}```

and here is my view:

```@extends('layouts.master')
@section('content')
<div class="col-sm-8 blog-main">
    <h1>{{ $post->title }}</h1>
    {{ $post->body }}
    <hr>
    <h5>Comments</h5>
    <div class="comments">
        <ul class="list-group">
            @foreach ($post->comments as $comment)
            <li class="list-group-item">
            <strong>
                {{ $comment->created_at->diffForHumans() }}: &nbsp;
            </strong>
            {{ $comment->body }}
            </li>
            @endforeach
        </ul>
    </div>
    <hr>
    <!-- Add Comment -->
    <div class="card">
        <div class="card-block">
            <form method="POST" action="/blog/public/posts/{{ $post->id }}/comments" >
                <div class="form-group">
                    <textarea name="body" placeholder="Your Comment" class="form-control"></textarea>
                </div>
                 <div class="form-group">
                     <button type="submit" class="btn btn-primary">Add Comment</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection```

Here is also my project link on GitHub:
https://github.com/HeshamAbusaif/Blog-Laravel




Please sign in or create an account to participate in this conversation.