【问题标题】:What is the best way to make a post page with comments. Laravel - vue.js - inertia-vue制作带有评论的帖子页面的最佳方式是什么。 Laravel - vue.js - 惯性 vue
【发布时间】:2020-03-08 15:45:02
【问题描述】:

我正在尝试与 cmets 建立一个讨论论坛,这就是它的样子

这个页面有路线

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 的回答,因为如果没有更多信息,它很难调试

标签: laravel vue.js inertiajs


【解决方案1】:

为了能够找到问题,您需要进行调试。我会做以下事情:

  • 在开发者控制台中,检查网络选项卡以检查请求以查看正在发送的 url 和数据。

  • 在控制台中运行 php artisan route:list 以查看是否列出了所有路由。也可以尝试使用php artisan route:cache清除缓存

  • 其他选项可能是更改资源路由 Route::resource('comments', 'ReplyController'); 为每个方法的各个路由,只是为了找到问题。

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 2022-01-01
    • 2013-10-04
    • 2023-04-09
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多