我正在努力在我的应用程序中显示 comments 回复.

在控制器中,我添加如下注释:

public function show( $slug )
{
    // Comments
    $commentsQuery  = $this->get_commentQuery( $article->id );
    $comments_count = $commentsQuery->count();

    // If infinite scroll, paginate comments (to be loaded one page per scroll),
    // Else show them all 

    if (boolval($this->is_infinitescroll)) {
        $comments = $commentsQuery->paginate($this->comments_per_page);
    } else {
        $comments = $commentsQuery->get();
    }

    return view( 'themes/' . $this->theme_directory . '/templates/single', array_merge(
        $this->data,
        [
            'categories'        => $this->article_categories,
            'article'           => $article,
            'old_article'       => $old_article,
            'new_article'       => $new_article,
            'comments'          => $comments,
            'comments_count'    => $comments_count,
            'comments_per_page' => $this->comments_per_page,
            'tagline'           => $article->title,
            'is_infinitescroll' => $this->is_infinitescroll
        ]
    ));
} 
/**
 * get_commentQuery
 *
 * @param int $article_id
 * @param int $limit
 * @param int $offset
 *
 * @return object
 */
private function get_commentQuery( int $article_id, int $limit = 0, int $offset = 0 ): object
{
    $commentQuery = Comment::where( [ 'article_id' => $article_id, 'approved' => 1 ] )
        ->orderBy( 'id', $this->comments_orderby_direction );

    if ( $offset > 0 ) {
        $commentQuery = $commentQuery->offset( $offset );
    }
    if ( $limit > 0 ) {
        $commentQuery = $commentQuery->limit( $limit );
    }

    return $commentQuery;
}  

在显示注释-comments-list.blade.php:fv的Blade 文件中

@foreach ($comments as $comment)
    @if (null == $comment->parent_id)
        <li class="depth-1 comment">
            <div class="comment__avatar">
                <img class="avatar" src="{{ asset('images/avatars/' . $comment->user->avatar) }}" alt="{{ $comment->user->first_name }} {{ $comment->user->last_name }}" width="50" height="50">
            </div>
            <div class="comment__content">
                <div class="comment__info">
                    <div class="comment__author">{{ $comment->user->first_name }} {{ $comment->user->last_name }}</div>
                    <div class="comment__meta">
                        <div class="comment__time">{{ date('jS M Y', strtotime($comment->created_at)) }}</div>
                        @auth
                            <div class="comment__reply">
                                <a class="comment-reply-link" href="#0">Reply</a>
                            </div>
                        @endauth
                    </div>
                </div>
                <div class="comment__text">
                    <p>{{ $comment->body }}</p>
                </div>
            </div>
            @auth
                @include('themes/' . $theme_directory . '/partials/comment-form')
            @endauth
    
            {{-- Comment replies --}}
            @if (count($comment->replies))
                <ul class="children">
                    @foreach ($comment->replies as $reply)
                        <li class="depth-2 comment">
                            <div class="comment__avatar">
                                <img class="avatar" src="{{ asset('images/avatars/' . $reply->user->avatar) }}" alt="{{ $comment->user->first_name }} {{ $comment->user->last_name }}" width="50" height="50">
                            </div>
                            <div class="comment__content">
                                <div class="comment__info">
                                    <div class="comment__author">{{ $reply->user->first_name }} {{ $reply->user->last_name }}</div>
                                    <div class="comment__meta">
                                        <div class="comment__time">{{ date('jS M Y', strtotime($reply->created_at)) }}</div>
                                    </div>
                                </div>
                                <div class="comment__text">
                                    <p>{{ $reply->body }}</p>
                                </div>
                            </div>
                        </li>
                    @endforeach
                </ul>
            @endif
        </li>
    @endif
@endforeach

如第$commentQuery = Comment::where( [ 'article_id' => $article_id, 'approved' => 1 ] )->orderBy( 'id', $this->comments_orderby_direction )行所示,所有 comments 都应该显示only if aproved.

这种情况确实发生在"家长" comments 中,但 comments 回复中有not条会出现这种情况.我如何才能将这一条件应用于回复?

推荐答案

  1. $comment->replies关系默认情况下无条件获取所有记录.您需要添加WHERE条件来解决此问题.

  2. 您可以通过添加以下条件来筛选已批准的 comments :

private function get_commentQuery( int $article_id, int $limit = 0, int $offset = 0 ): object
{
    $commentQuery = Comment::where( [ 'article_id' => $article_id, 'approved' => 1 ] )
        ->orderBy( 'id', $this->comments_orderby_direction )
        ->with('replies', function($query){
             $query->where('approved', 1);
        });

    if ( $offset > 0 ) {
        $commentQuery = $commentQuery->offset( $offset );
    }
    if ( $limit > 0 ) {
        $commentQuery = $commentQuery->limit( $limit );
    }

    return $commentQuery;
} 

当获取Blade 文件中的$comment->replies时,上述查询也应该解决您的N+1查询问题

Php相关问答推荐

如何使用PHP停止foreach循环中的重复项?

在WooCommerce中处理客户总支出中的退款

为什么调用干预\映像引发错误驱动程序\GD\Driver not found?

我如何知道BaseController中当前运行的是哪个控制器?

使用PHP阅读XML提要

Laravel:如何展平多维Eager 加载的集合

在Laravel中从动态嵌套数组中获取值

在php中,方法之间的属性链接是如何工作的

PHP按另一个二维数组的排序顺序对二维数组进行排序

调度程序不发送任何消息

使用 wp_footer 挂钩添加一些 CSS 不起作用

使用 phpseclib 验证 RSA-PSS

如何在 PHP 中对具有相同数组值的值求和

从 Laravel 机制中排除目录并直接访问该目录

PHP / DOM : 解析 HTML 以提取基于类的数​​据

Laravel Docker 几个数据库

遇到特定键时修改二维数组以创建嵌套数据集

PHP - 如何获取发送到脚本的查询参数

将参数传递给 laravel 9 工厂

服务器发送的事件:EventSource 抛出 MIME 错误