错误出现问题:此路由不支持GET方法。支持的方法:POST

时间:2019-08-06 14:55:04

标签: php laravel

出现错误问题

  

此路由不支持GET方法。支持的方法:POST。

我输入了路由发布方法和表单,但是idk为什么它仍然显示错误...

//route
Route::post('posts/{post}/comment', 'CommentController@store'); 

//controller
class CommentController extends Controller
{
    public function store(Request $request, $post_id)
    {
        $this->validate($request,[
            'content' => 'required',
        ]);
        //$post = Post::find($post_id);
        $comment = new Comment;
        $comment->content = $request->input('content');
        $comment->user_id = auth()->user()->id;
        //$comment->post()->associate($post);
        $comment->save();

        return redirect('/posts')->with('success','Post Created');
    }
}

//form
{{ Form::open(['method' => 'POST','action' => ['CommentController@store', $post->id]]) }}
<div class="row">
    <div class="col-md-12">
        {{ Form::label('comment', "Comment:") }}
        {{ Form::textarea('content', null, ['class' => 'form-control']) }}
        {{ Form::submit('Add Comment', ['class' => 'btn btn-success']) }}
    </div>
</div>
{{ Form::close() }}

// web.php

Route::get('/', 'PagesController@index');

Route::get('/about', 'PagesController@about');

Route::get('/services', 'PagesController@services');

Route::resource('posts','PostsController');

Route::post('/posts/{post}/comment', 'CommentController@store');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

我想在发布/显示视图中按下按钮添加新评论时,将其发送到需要发送的地方,而这只会给我一个错误。

2 个答案:

答案 0 :(得分:0)

您在$post中尝试过public function store(Request $request, $post_id)吗?

答案 1 :(得分:0)

请如下更改。请确保在路由中使用resource时,它应始终位于其他相关路由的下方。调用路由时,它将调用posts/{$id}/edit,这是get方法。有关更多信息,请检查以下博客。 https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

Route::resource('posts','PostsController');
Route::post('/posts/{post}/comment', 'CommentController@store');

Route::post('/posts/{post}/comment', 'CommentController@store');
Route::resource('posts','PostsController');