【问题标题】:get comments on a post from database using ajax使用ajax从数据库中获取对帖子的评论
【发布时间】:2019-06-14 13:47:20
【问题描述】:

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

这是问题

我在home 路由中从数据库中获取了所有posts,但我没有在这些帖子中获取comments

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

评论是通过 Ajax 成功添加的,但我无法使用 Ajax 从数据库中获取它们。

这是我的代码

控制器

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

index方法在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 ) );
}

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

** Form留下评论**

@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

当我点击 Comment 按钮时,会运行以下代码将评论插入到comments 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>")

        }
    });
});


});

cmets 已成功添加到图片中提到的表格中,并且我已经以上述形式将它们取回,但是当我刷新页面时会提取它们,我想在有人发表评论时获取它们飞,因为它是通过ajax插入,但刷新页面后获取。

更新

/comment 代码在这里

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';
        }

}

带有“虚拟”数据的 cmets 表 looks like this 我正在尝试从过去 2 天开始解决此问题,请帮助。 谢谢

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

【问题讨论】:

  • 你用php吗?里面有回报吗?
  • 您好,感谢您的回复,我正在使用 Laravel @bakero98
  • 您收到 cmets 的响应了吗?请在发送 ajax 请求的地方添加“/comment”代码。
  • 我已经更新了帖子,请查看@ShivendraSingh

标签: javascript php jquery ajax


【解决方案1】:

表单代码更新。

@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>");

        }
    });

【讨论】:

猜你喜欢
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
相关资源
最近更新 更多