遇到错误问题:此路由不支持 GET 方法.支持的方法:开机自检



有错误问题:

此路由不支持 GET 方法。支持的方法:开机自检。

我输入了路由帖子方法和形式,但 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() }}

//网络.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');

我希望当我按下按钮在帖子/显示视图中添加新评论时,将我发送到需要发送的地方,它只会给我一个错误。

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

请按如下方式更改。 请确保无论何时在路由中使用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');

相关内容

最新更新