thinkphp6实现仿微信朋友圈,用户可发布图片和文字内容,用户可评论,其他用户可评论文章,也可回复用户评论,多层级评论,无限级评论

功能:仿微信朋友圈,用户可发布图片和文字内容,用户可评论,其他用户可评论文章,也可回复用户评论,多层级评论,无限级评论

数据库示例:

朋友圈内容表 article表:

id content image likeNum(点赞数) member_id(发布用户id)

评论表 comment表:

id forum_id comment_id comment_content member_id(评论用户id)

说明:

1.查看article表的下级评论 查询comment的forum_id符合条件的数据

2.查看用户评论用户的信息,递归依次查询 查询comment的comment_id符合条件的数据

thinkphp6代码示例:

public function article(){

//查询出所有数据

$articles = Db::name('article')

->select();

$newArticles = [];

foreach ($articles as $item) {

$newArticles = $item;

$newArticles['comments'] = Db::name('comment')

->where('forum_id',$item['id'])

->select()

->toArray(); //需要继续遍历处理下级评论,需要转为数组

// 处理评论的子评论

foreach (newArticles\['comments'\] as \&comment) {

$comment['child_comments'] = this-\>getChildComments(comment['id']); //调用方法遍历下级评论

}

$newArticles[] = $newArticles;

}

echo json_encode($newArticles);

}

// 遍历获取子评论

function getChildComments($commentId)

{

$childComments = Db::name('comment')

->where('comment_id', $commentId)

->select()

->toArray();

if (empty($childComments)) {

return [];

}

foreach (childComments as \&childComment) {

$childComment['child_comments'] = this-\>getChildComments(childComment['id']);

}

return $childComments;

}