首页 > 解决方案 > Laravel:morphMany 可以递归评论/回复?

问题描述

我正在寻找最节省资源和可扩展的方式来检索构建多模型数组所需的数据——发布、用户、喜欢、评论——在包含递归评论(评论)的社交媒体应用程序中发布对象到评论)。我最近开始尝试 morphMany 关系,但看不到它们如何与递归评论一起工作。

在我的 PostController 中,我$posts使用此查询返回一个数组:

$posts = Post::whereIn('user_id', $friendIds)->with([
            'comments.user:id,name', 
            'comments.likes.user:id',
            'comments.comments.user:id,name', 
            'comments.comments.likes.user:id', 
            'user', 
            'likes.user:id'])
            ->get();

注释表具有以下结构:

  1. ID
  2. 用户身份
  3. commentable_id
  4. 可评论类型
  5. 身体
  6. 时间戳

我正在使用一个特征来抽象评论之间的 morphMany 关系,这comments.comments是可能的。但我无法弄清楚或找到任何地方如何递归地做到这一点。

Laravel 的 morphMany 关系以及特征是否与递归注释兼容?

标签: laravelrecursioneloquentpolymorphic-associations

解决方案


推荐阅读