微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 使用ajax从数据库获取对帖子的评论

我正在尝试使用ajax从数据库获取每个帖子的所有注释,但我遇到了一些问题.

这是问题所在.

我已经回家了,我从数据库获取了所有帖子,但我没有在这些帖子上发表评论.

现在我想使用Ajax为每个帖子获取评论,例如当某人在帖子上发表评论时,应将其添加数据库中,然后即时从数据库获取.

注释是通过Ajax成功添加的,但是我无法使用Ajax从数据库获取它们.

这是我的代码

调节器

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

HomeController里面的索引方法

   public function index()
{

    $post  = Post::all();

    // I've used this approach to get comments
    //$posts = Post::with('comments')->get();


    return view('home')->with( array('posts'=>$posts ) );
}

我已经遍历了数据库中的所有帖子,所以我还在那个循环中有一个表单来为该帖子发表评论.

**表格留下评论**

@foreach($posts as $post)
   <p>$post->description</p>

   <div class="show_comments_{{ $post->id }}"></div> 



  <form action="{{ url('comment',$post->p_id)  }}" method="POST"  > 
   @csrf
<textarea 
   name="body"
   placeholder="Write A Suggestion" 
   data-autosize-input='{ "space": 100 }' 
   rows="50" cols="100"
   name="comment"  
   class="form-control comment body">

 </textarea> 
 <input type="hidden" value="{{csrf_token()}}">


    <!-- user Id of the logged In one -->

  <input type="text" class="user_id" value="{{Auth::user()->id}}" name="user_id">

        <!-- post id  -->

  <input type="text" class="post_id" name="post_id" value="{{$post->p_id}}">



   <button type="button" class="btn btn-sm btn-danger formcomment" value = 'Comment' style="margin:10px;">Comment</button> 
</form>


@endforeach

当我单击“注释”按钮时,将运行以下代码以将注释插入注释table中.

Ajax请求

$(document).ready(function(){

     $('.formcomment').on('click', function(e) {
    e.preventDefault();

    var form = $(this).closest('form');
    // these are id's

    var body = form.find('.body').val();
    var post_id = parseInt(form.find('.post_id').val());
    var user_id = parseInt(form.find('.user_id').val());

    // alert(body);
    // alert('this is post'+post_id);
    // alert('This is user id'+user_id);


    $.ajax({
        type: "POST",
        url: '/comment/'+post_id,
        data: {body:body, post_id:post_id, user_id:user_id,  _token: '{{csrf_token()}}'},
        success: function(data) {


            $(".show_comments_"+post_id).append("<div style = 'color:red;'>"+data.msg+"</div>");
          $(".name_"+user_id).append("<div>"+data.user_id+"</div>")

        }
    });
});


});

注释被成功添加到图像中提到的表中,并且我已经以上面提到的形式将它们取回,但是当我刷新页面获取它,我想在有人即时发表评论获取它们,因为它是通过ajax插入,但刷新页面获取.

更新

/评论代码在这里

public function storeComments(Request $request,Comment $body,$post_id){


 if($request->ajax()){
    $comment = new Comment;
    $comment->user_id =  Auth::user()->id;
    $comment->post_id = $post_id;
    $comment->body = Input::get('body');

    $comment->save();
    $response = array(
                'status' => 'success',
                'msg' => 'Setting created successfully',
            );
            return Response::json($response);
            return 'yes';
        }else{
            return 'no';
        }

}

带有’dummy’数据的评论looks like this
我正试图解决这个问题,过去2天,请帮忙.
谢谢

我为此研究过的一个链接this

解决方法:

表单代码更新.

@foreach($posts as $post)
   <p>$post->description</p>

    <div class="show_comments_{{ $post->p_id}}"></div>

  // I found this code on laracast, at end there is link.
   @if (count($post->comments))
       @foreach($post->commnents as $comment)
         <small>$comment->body</small>
       @endforeach
    @else 
        No comments Found
  @endif 

将数据存储在json响应中.

if($request->ajax()){
    $comment = new Comment;
    $comment->user_id =  Auth::user()->id;
    $comment->post_id = $post_id;
    $comment->body = Input::get('body');

    $comment->save();
   //add the comment in response 
    return response()->json(['msg'=>$comment->body]);
}

在ajax中需要显示我们已成功添加数据的消息

$.ajax({
        type: "POST",
        url: '/comment/'+post_id,
        data: {body:body, post_id:post_id, user_id:user_id,  _token: '{{csrf_token()}}'},
        success: function(data) {
            //get the msg from success response. 
               $(".show_comments_"+post_id).append("<div style = 'color:red;'>"+data.msg+"</div>");

        }
    });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐