我有一个像这样的评论表
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 detail($id){
$comments = Review::whereNull('parent_id')->orderBy('id', 'asc')->get();
$result = array();
foreach($comments as $comment){
$list = array();
$list = array_merge($list, [['id' => $comment->id, 'parent_id' => $comment->parent_id, 'review' => $comment->review]]);
//return $list;
$result = array_merge($result, $this->get_child_comment($comment->comment_id,0));
//return $result;
}
return view('pages.product_detail', ['detail_dir_imgs'=>$detail_dir_imgs ,'seller'=>$seller, 'product_detail' => $product_detail, 'last4products'=>$products, 'sim_products'=> $sim_products,'reviews'=>$result,'getProduct'=>$getProduct]);
}
public function get_child_comment($pid,$level,$list=array()) {
$sub_comments = Review::where('parent_id','=',$pid)->where('id','!=',$pid)->orderBy('id', 'asc')->get();
foreach($sub_comments as $sub_comment){
$space = " ";
$sigm='-';
for($j=0; $j<=$level; $j++)
{
$space .=$space;
}
for($j=0; $j<=$level; $j++)
{
$space .= $sigm;
}
$sub_comment->comment = html_entity_decode($space, ENT_QUOTES, "utf-8").' '.$sub_comment->comment;
$list = array_merge($list, array(['id' => $sub_comment->id, 'parent_id' => $sub_comment->parent_id, 'review' => $sub_comment->review]));
$list = $this->get_child_comment($sub_comment->id, $level+1, $list);
}
return $list;
}
存储评论的功能:
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
我希望输出像图像