如何在Laravel中限制回复评论的深度

时间:2019-04-21 05:36:42

标签: php laravel comments

我可以通过递归方式创建无限的回复评论。这件事可能很糟糕,因为我的网络看起来很丑。

我希望用户仅可以回复评论最多3个回复评论的深度,因此,如果该深度比最大深度更大,则评论将不再创建嵌套。

这是到目前为止我可以尝试的代码,可以在blog.detail.php中显示注释

@include('nested.replies', ['comments' => $post->comments, 'post_id' => $parameter, 'depth' => 0])

和replies.php

@foreach($comments->sortByDesc('created_at') as $comment)

<?php
$parameter = Hashids::connection()->encode($post->id);
$parameter2 = Hashids::connection()->encode($comment->id);
?>

<ol class="comments-list display-comment">
    <li>
        <div class="comment-box clearfix" >

            <div class="avatar"><img alt="" src="{{ asset('frontboard/images/avatar.png') }}" /></div>
            <div class="comment-content" id="reply?{{ $parameter2 }}">
                <div class="comment-meta">
                    <span class="comment-by">{{ $comment->user->name }}</span>
                    <span class="comment-date">{{ $comment->date }}</span>
                </div>
                <span class="reply-link" stle="padding-top:-220px;">
                    <p>{{ $comment->body }}</p>

                    @if(Auth::guard('member')->check() && Auth::guard('member')->user()->active == 1)
                        @if(Auth::guard('member')->user()->isBan == 1)
                        <span class="reply-link">
                            Blocked
                        @else
                            {!! Form::open(['method' => 'DELETE','route' => ['comment.destroy', $comment->id]]) !!}
                            {!! Form::submit('delete', ['onclick' => 'return confirm("Are you sure?");','class' => 'btn btn-danger btn-xs pull-right'])!!}
                            {!! Form::close() !!}

                            <a href="#" class="replyform">Reply</a>
                            {!! Form::open([
                                'route' => ['reply.add', $post->slug],
                                'class' => 'showActionComment',
                                'data-parsley-validate'])
                            !!}

                            @csrf
                            <div class="form-group {{ $errors->has('comment_body') ? 'has-error': '' }}">
                                {!! Form::textarea('comment_body', null, [
                                    'name'        => 'comment_body',
                                    'class'       => 'form-control',
                                    'placeholder' => 'Write Something.. ',
                                    'required', 'minlength="4"'])
                                !!}
                                @if($errors->has('comment_body'))
                                    <span class="help-block">{{$errors->first('comment_body')}}</span>
                                @endif

                                {!! Form::hidden('post_id', $parameter) !!}
                                {!! Form::hidden('comment_id', $parameter2) !!}


                            </div>
                            <div class="form-group">
                                <input type="submit" id="postBtn" value="Add Comment" />
                                <input type="button"  class="btn1 btn-warning" value="Hide" />
                            </div>
                            {!! Form::close() !!}
                        @endif
                    @endif
                </span>

            </div>
        </div>
      @if($depth < 3)
        <ul>
            <li>

                @include('nested.replies', ['comments' => $comment->replies, 'depth' => $depth + 1])

            </li>
        </ul>
        @endif
    </li>
</ol>
@endforeach

我们可以在此处查看回复

@if($depth < 3)
        <ul>
            <li>

                @include('nested.replies', ['comments' => $comment->replies, 'depth' => $depth + 1])

            </li>
        </ul>
        @endif

我认为这段代码无法正常工作,因为它仍然执行嵌套注释。

0 个答案:

没有答案