2017-05-08 105 views
1

我有这样Laravel 5.4答复父评论,并显示父评论下

mysql> select * from review; 
+----+----------+--------+------------+---------------------+---------------------+----------+------+---------+-----------+ 
| id | review | rating | product_id | created_at   | updated_at   | approved | spam | user_id | parent_id | 
+----+----------+--------+------------+---------------------+---------------------+----------+------+---------+-----------+ 
| 1 | how much |  2 |   25 | 2017-05-05 12:02:02 | 2017-05-05 12:02:02 |  1 | 0 |  10 |  NULL | 
| 2 | 20  | NULL |   25 | 2017-05-05 12:02:35 | 2017-05-05 12:02:35 |  1 | 0 |  10 |   1 | 
| 3 | ok  | NULL |   31 | 2017-05-08 05:09:01 | 2017-05-08 05:09:01 |  1 | 0 |  9 |   2 | 
| 4 | 30  | NULL |   31 | 2017-05-08 05:25:47 | 2017-05-08 05:25:47 |  1 | 0 |  9 |   3 | 
| 5 | 40  | NULL |   31 | 2017-05-08 05:26:21 | 2017-05-08 05:26:21 |  1 | 0 |  9 |   4 | 
| 6 | not fair | NULL |   31 | 2017-05-08 05:27:16 | 2017-05-08 05:27:16 |  1 | 0 |  9 |   5 | 
| 7 | 80  | NULL |   31 | 2017-05-08 05:29:26 | 2017-05-08 05:29:26 |  1 | 0 |  9 |   1 | 
| 8 | hi  | NULL |   29 | 2017-05-08 05:37:25 | 2017-05-08 05:37:25 |  1 | 0 |  9 |  NULL | 
+----+----------+--------+------------+---------------------+---------------------+----------+------+---------+-----------+ 

的检查表,当我尝试。它正确地存储他们的父母的ID我想要添加注释的注释现在是显示在其父评论评论...

我怎样才能做到这一点?

在我的控制,我使用2个功能

函数来显示评论:

功能存储回顾:

public function review($id,Request $request){ 
     $userid = Auth::user()->id; 
     $product = Product::find($id); 
     $product = Product::find($id); 
     $input = array(
      'review' => Input::get('review'), 
      'rating' => Input::get('rating'), 
      'parent_id' => Input::get('parent_id') 
     ); 
     $review = new Review; 
     $validator = Validator::make($input, $review->getCreateRules()); 
     if ($validator->passes()) { 
      $review->storeReviewForProduct($id, $input['review'], $input['rating'],$input['parent_id']); 
      return redirect('product-detail/'.$id.'#reviews-anchor')->with('review_posted',true); 
     } 
     return Redirect::to('product-detail/'.$id.'#reviews-anchor')->withErrors($validator)->withInput(); 
    } 

这是我的看法:

@foreach($reviews as $val) 
       <div class="row"> 
        <div class="col-md-12"> 
        <p>{{$val['review']}} <span class="reply-comment">Reply</span></p> 
         {{-- Start Comment --}} 
        <div class="comment-box" style="display:none;"> 
         <form action="{{ route('product.review',['id'=>$getProduct->id]) }}" method="post" id="comment"> 
         {{csrf_field()}} 
         <input type="hidden" value="{{$val['id']}}" name="parent_id" id="parent_id"> 
         <textarea class="form-control animated" cols="50" id="new-comment" name="review" placeholder="Leave your comment here" rows="5"></textarea> 
         <div class="text-right"> 
          <button class="bc-btn-cancel-review" href="#" id="close-comment">Close</button> 
          <button class="bc-btn-save-review" type="submit" id="review">Comment</button> 
         </div> 
         </form> 
        </div> 
        {{-- End Comment --}} 
       </div> 
       </div> 
      @endforeach 

我要像图像

enter image description here

+1

向我们展示你的模型没有定义任何关系?你怎么传达你的观点。 –

+0

你可以显示你迄今为止工作过的代码吗? –

+0

编辑我的代码......可以请你检查一下 – Karthiga

回答

0
You can do something like this, 
disregard the syntax, check the logic, 
you can improve it by creating a query that will display all comments_id related to its parents and looping until the last associated parent_id is executed, 

HTML 
     <div class="the_topic"> 
     </div> 
     <div class="comments_here"> 
     </div> 

    $.ajax({  
       type: "POST", 
       url: 'url_to_controller/function/model_to_display_comment', 
       data: comment_id 
       dataType: json 
       success: function (data) { 
          $.each(data, function(a,b){ 
            $("comments_here").append("<div id='"+b.id+"'>"+b.review+"</div>"); 

           b.parent_id = null : '' ? 
      $.ajax({  
       type: "POST", 
       url: 'url_to_controller/function/model_to_display_comment', 
       data: b.parent_id 
       dataType: json 
       success: function (data) { 
          $.each(data, function(c,d){ 
            $("."+d.parent_id).append("<div id='"+d.id+"'>"+d.review+"</div>");        

          })  


         }, 
       }); 
+0

能否请您解释一下它更 – Karthiga

0

输出你做错了实现这一目标使用的hasMany关系的最简单方法。 制作另一个名为subCommet的表。

------------------------------------------------------------------ 
|id | parent_id | comment | created_at | updated_at | 
------------------------------------------------------------------ 

现在,在您Review.php模型中定义的关系

public function subcomments() 
{ 
return $this->hasMany('App\SubComment', id , parent_id); 
} 

然后在视图模板访问subcomments

@foreach($reviews as $val) { 
<div id="comment">{{$val->review}}</div> 
@if($val->subcomments->count() != 0) 
    @foreach($val->subcomments as $subcomment) 
     <div id="sub-comment">{{ $subcomment->comment }}</div> 
    @endforeach 
@endif 
@endforeach 

你需要的是与评论的隐藏字段和回复文本区域,后评论发表的用户,你需要在你的控制器,以检查其天气的评论或一些评论的回复,然后根据意见类型,如果注释是一些共同的答复保存在意见表然后将它存储在子注释表中,否则将其存储在Review表中。

+0

当我尝试这一点,显示了一个错误'试图让非object' – Karthiga

+0

更换物业'返回$这个 - >的hasMany(“应用\ SubComment”,ID,PARENT_ID);'和'$返回这 - >的hasMany( '应用程序\ SubComment',PARENT_ID,ID);' – Sama

+0

同样errror – Karthiga