簡體   English   中英

如何顯示按相似帖子分組的所有評論和回復

[英]How to show all comments and replies grouped by similar post

我想基於類似的commentable_id(帖子)創建一組評論並顯示它們。

我的問題是我無法顯示所有回復評論 我只是表明評論的根據,而不是為了答復

我的控制器顯示評論

public function index()
{
    $mycomments = Comment::where('user_id', '=', Auth::user('member')->id)
                            ->groupBy('commentable_id')
                            ->with('replies')
                            ->orderBy('created_at', 'desc')
                            ->get();

    return view('frontendblog.memberlogin.home', compact('mycomments'));
}

和我的評論模型

public function user()
{
    return $this->belongsTo(Member::class);
}

public function post()
{
    return $this->belongsTo(Post::class);
}


public function myreplyfrom()
{
    return $this->belongsTo(Comment::class,'parent_id');
}

public function commentable()
{
    return $this->morphTo();
}

public function replies()
{
    return $this->hasMany(Comment::class, 'parent_id');
}

我的評論表是這樣的 我的評論表

我的代碼在看來

@foreach($mycomments as $comment)

                        <a href="{{ route('blog.show', $comment->commentable->slug) }}#comments"> <p>Title Post->{{ $comment->commentable->title }}</p> </a>
                        <p> Author of Post-> {{ $comment->commentable->author->name }} </p>
                        <p>Comment->{{ $comment->body }}</p>
                        @if($comment->myreplyfrom)
                            <p> Reply from Comment-> {{ $comment->myreplyfrom->body }} </p>
                        @endif
                        {!! Form::open([
                            'method' => 'POST',
                            'route' => ['comment.destroy',"id_comment" => $comment->id, "id_member" => $comment->user_id]])
                        !!}
                        {!! Form::submit('delete', [
                            'onclick' => 'deleteConfirm()',
                            'id' => 'from1',
                            ])!!}
                        {!! Form::close() !!}
                        <hr/>
                    @endforeach

我的結果是這樣的

Title Post-> Quasi itaque perferendis aut animi magnam incidunt qui debitis et nesciunt假定為saepe。

Post-> Ilham Firman A的作者

注釋->動作1(刪除)


職稱發布->在inciuntant Provident aque atque qui。

Post-> Ilham Firman A的作者

評論-> woww(刪除)

預期的結果

Title Post-> Quasi itaque perferendis aut animi magnam incidunt qui debitis et nesciunt假定為saepe。

Post-> Ilham Firman A的作者

注釋->動作1(刪除)

評論->回復動作1(刪除)


職稱發布->在inciuntant Provident aque atque qui。

Post-> Ilham Firman A的作者

評論-> woww(刪除)

評論-> sjksjks(刪除)

注釋-> sdsds(刪除)

問題是此行:

->groupBy('commentable_id')

當您按commentable_id分組時,評論表中的每個帖子將只接受一個評論。

您可以執行以下操作:

// This is a collection of comments grouped by post id, so I rename the variable to $commentsGroupedByPost.
$commentsGroupedByPost = Comment::where('user_id', '=', Auth::user('member')->id)

                            ->with('replies', 'commentable')
                            ->orderBy('created_at', 'desc')
                            ->get()
                            ->groupBy('commentable_id');

然后從視圖中:

@foreach($commentsGroupedByPost as $postId => $comments)
    <a href="{{ route('blog.show', $comments->first()->commentable->slug) }}#comments">
        <p>Title Post->{{ $comments->first()->commentable->title }}</p> 
    </a>
   <p> Author of Post-> {{ $comments->first()->commentable->author->name }} </p>

   @foreach($comments as $comment)

        <p>Comment->{{ $comment->body }}</p>
        @foreach($comment->replies as $reply)
            <p> Reply from Comment-> {{ $reply->body }} </p>
        @endforeach
        {!! Form::open([
            'method' => 'POST',
            'route' => ['comment.destroy',"id_comment" => $comment->id, 
            "id_member" => $comment->user_id]])
        !!}
        {!! Form::submit('delete', [
            'onclick' => 'deleteConfirm()',
            'id' => 'from1',
        ])!!}
        {!! Form::close() !!}
    @endforeach
    <hr/>
@endforeach

我不確定您要達到的目標是100%,但是我認為您只是想獲得第一條評論/答復?

如果是這樣,請嘗試以下操作:

$mycomments = Comment::where('user_id', '=', Auth::user('member')->id)
        ->groupBy('commentable_id')
        ->with(['replies', function($query) {
            $query->latestFirst();
        }])
        ->get();

我不確定您的latestFirst()方法在做什么,但是我添加的閉包應該為您提供回復/評論的第一個實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM