【发布时间】:2020-03-08 15:45:02
【问题描述】:
这个页面有路线
Route::get('forums/comment/{id}', 'ForumController@comment');
在控制器中
public function comment($id){
$forum = DiscussionForum::where('id', $id)
->with('comment', 'user')->first();
$comment = Reply::with('discussionForum', 'user')
->where('discussion_forum_id', $forum->id)
->orderByDesc('updated_at')->get();
return Inertia::render('Forum/Comment.vue', [
'forum' => $forum,
'comments' => $comment
]);
}
我的问题是如何将此评论保存在评论控制器中,在我的 vue 文件中,我有这个方法,当我按下保存按钮时会执行
submit() {
this.$refs.form.validate((valid) => {
if (valid) {
if (!this.form.id) {
this.$inertia.post('comments', {
comment: this.form.comment,
forum_id :this.form.id,
}).then(() => this.refresh())
}
} else {
return false;
}
this.reset();
});
},
我也有这条路线供 cmets 使用
Route::resource('comments', 'ReplyController');
对于商店功能我有这个
public function store(Request $request)
{
DB::beginTransaction();
$comment = new Reply();
$comment->user_id = auth()->user()->id;
$comment->discussion_forum_id = $request->forum_id;
$comment->comment = $request->comment;
$comment->comment_time = Carbon::now();
$comment->save();
DB::commit();
}
问题是当我尝试保存评论时收到此错误
此路由不支持 POST 方法。支持的方法:GET、HEAD。
我该如何解决这个问题?或者有什么更好的方法来做到这一点?
【问题讨论】:
-
出于某种原因,我怀疑您正在向另一条路线提出请求。您能否确认您确实在向 /cmets 发出请求?
-
表单上的 action 属性是什么?
-
@Charlie 我正在尝试保存来自此 url
/forums/comment/{id}的评论并尝试将其发送到这里comments以使用回复控制器 (CommentController) 中的存储功能保存它 -
您应该关注@iCrashOne 的回答,因为如果没有更多信息,它很难调试