【问题标题】:Laravel commenting with ajaxLaravel 使用 ajax 进行评论
【发布时间】:2018-12-18 23:46:37
【问题描述】:

所以我有一个 laravel 评论系统,可以让我使用 ajax 进行评论。我目前的设置很简单。我有一个 cmets 字段,然后我的路线如下:

Route::post(‘comment/{post_id}’, ‘CommentController@insert’);

在我的 ajax url 中,我给出了与 post_id 相同的路由。我提供帖子 ID 是因为我想将帖子 ID 添加到我的 cmets 表中的 post_id 列中。我的ajax也符合要求。

现在我的问题是,我不知道如何在评论中添加回复。我必须将comment_id 插入到我的回复表comment_id 列,因为评论和回复是相关的。让我感到困惑的是,如果我为每个评论创建了很多带有 foreach 循环的回复表单,我如何将所有这些评论 ID 传递给 ajax? 举个例子,这是我存储回复的路径

Route::post(‘replies/{comment_id}’, ‘ReplyController@insert’);

这不会像 cmets 那样只传递参数 (post_id) 的值。这个回复对于一个参数会有很多值,对吧?那么我该如何进行呢。我是 ajax 新手,我很难理解这个逻辑。就像我之前提到的,令人困惑的是每个回复都会有一个单独的comment_id,我需要将它传递给路由参数。

【问题讨论】:

  • 你不是在路由中给post_id,而是在请求中。所以分享你的 ajax 请求?

标签: ajax laravel laravel-5.6


【解决方案1】:

你应该像下面这样通过:

Route::post('/comment/{comment_id}/replies','ReplyController@insert');

【讨论】:

  • 这个我得到了。但是你能帮我处理ajax吗?我如何编写ajax请求的url?我的意思是,每条评论都有很多回复,对吧?那么我如何告诉 ajax 我正在传递哪个comment_id?这有点令人困惑。
  • 从视图中传递 data-url,例如:data-url="/comment/{{$comment->id}}/replies" 然后在 js 文件中获取它,例如
  • var url = $(this).attr('data-url');并将 var url 放入 ajax 的 url 部分。
  • 所以为了澄清,data-url= 进入表单,对吗?所以我循环回复表单并在每个表单中使用 data-url 输出评论 id。
  • 是的,我明白这一点。但是不应该有提交的按钮吗?用户必须能够单击按钮吗?他们还会怎么发布呢?
【解决方案2】:

你可以这样试试

查看(这里假设$comments$comment->replies,你可能有不同)

<div class="post-comments">
    <p>Comments</p>
     @foreach($comments as $comment)
        <p>{{$comment->text}}<p>

        <label>Replies:</label>
        <ul>
          @foreach($comment->replies as $reply)
            <li>{{$reply->text}}</li>
          @endforeach
           <form name="replyForm">
               <input name="reply" />
               <button type="button" onclick="replyComment('/comment/{{$comment->id}}/reply', this.form.reply)">Reply</button>
          </form>
        </ul>
     @endforeach
  </div>

Javascript

function replyComment(url, input){

   console.log(url);
   console.log(input.value);
   //call ajax with this url and input value
}

路线

Route::post('comment/{comment_id}/reply', 'ReplyController@insert);

【讨论】:

    【解决方案3】:
    <input type="submit" style="float: right;" class="btn btn-primary" value="Comment" id="comment" data-url="/comment/{{$comment->id}}/replies" data-token="{{ csrf_token() }}" data-comment_id="{{$comment->id}}" >
    

    假设您正在从控制器获取 $comment。 即使您正在添加按钮,也无需添加&lt;form&gt;

    【讨论】:

    • 所以现在只需要提供两个输入标签。用户名和回复。是吗?
    • @PICKAB00 用户id你可以从auth获取,不需要在表单中添加,你试过我的回答吗
    • 目前远离电脑。我一回来就试试
    • @rkj 对不起,让我解释一下。整个概念是让客人能够添加 cmets 并回复 cmets。这就是为什么我说用户名。所以在这种情况下我不需要 auth()。
    【解决方案4】:

    回复时必须通过post_id

    路线如下:

    Route::`post(‘replies/{post_id}/{comment_id}’, ‘ReplyController@insert’);`
    

    然后通过插入 time 对这些评论进行排序。

    【讨论】:

    • 抱歉,我想知道的是如何传递comment_id。我的意思是,在一篇文章中,会有很多 cmets 对吗?我如何告诉 ajax 这个回复属于特定评论?换句话说,我怎样才能告诉 ajax 这个回复会发给哪个评论?每个评论都会有一个ID,对吗?我应该在ajax url中输入什么?
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2014-04-19
    • 1970-01-01
    相关资源
    最近更新 更多