overtrue / laravel-like

👍 User-like features for Laravel Application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

在嵌套评论里使用Like出现多条数据重复加载的问题?

f4xy opened this issue · comments

commented

解决不了问题,还是自己开发吧。谢谢啦

建议你把代码贴全,另外代码的格式是:

```php
代码贴这里
```

从代码看应该是子评论部分导致

//在子评论中attachLikeStatus,以便使用has_liked状态
auth()->user()->attachLikeStatus($comment->children;)
  1. 评论改成平级
  2. 两个包不会出现 N+1

还有一个暴力的方案:

$comments = $this->model->comments()
                            ->with('likers', 'user', 'children.user','children.children.likers', 'children.likers')
                            ->withCount(['children','likers'])
                            ->parent()
                            ->latest()
                            ->paginate(15);

不过还是建议你的评论不要这样设计三层结构,无线层级也应该是扁平拉取然后再组装