Laravel实现小红书式二级评论+游标分页分页封装

抖音和小红书的评论都是两级的,抖音需要点开才能查看二级回复,小红书默认每条评论展开一条二级评论,这里以小红书作为实现目标。

表结构

CREATE TABLE `comment` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `post_id` bigint unsigned NOT NULL,
  `user_id` bigint unsigned NOT NULL,
  `content` text COLLATE utf8mb4_unicode_ci,
  `like_count` bigint NOT NULL DEFAULT '0',
  `status` int NOT NULL DEFAULT '0',
  `sub_comment_count` bigint unsigned NOT NULL DEFAULT '0',
  `updated_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `comment_reply` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `comment_id` bigint unsigned NOT NULL,
  `user_id` bigint unsigned NOT NULL,
  `reply_user_id` bigint unsigned NOT NULL DEFAULT '0',
  `content` text COLLATE utf8mb4_unicode_ci,
  `updated_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`,`comment_id`,`user_id`,`reply_user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

模型定义

// 评论表模型
class Comment extends Model
{
    use HasFactory;

    protected $table = 'comment';

    // 实时读取回复(二级评论)数量
    public function replies()
    {
        return $this->hasMany(SubComment::class, 'comment_id');
    }

    public function sub_comments()
    {
        return $this->hasMany(SubComment::class, 'comment_id')->orderBy('created_at', 'asc');
    }

    // 访问器 是否包含更多回复
    protected function subCommentHasMore(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->sub_comment_count>1 ? true : false,
        );
    }

}

// 二级评论表模型
class SubComment extends Model
{
    use HasFactory;

    protected $table = 'comment_reply';
}

业务代码

class CommentController extends Controller
{

    /**
     * http://domain/comment/page
     */
    public function page(Request $request)
    {
        $post_id = $request->input('post_id', 1);

        /**
         * Laravel的关联模型with时已经做了优化,这段代码两表各查询一次,不会因为有几条评论就查询二级评论表多次
         */
        $query = Comment::where('post_id', $post_id)
                    ->with(['sub_comments' => function ($query) {
                        // 每条评论查出一条二级回复出来
                        // 可以在这个$query按自己的需求排序,比如查询点赞最多的二级评论
                        $query->orderBy('created_at', 'asc')->limit(1);
                    }]);

        // 返回结果
        return response()->json([
            'code' => 0,
            /*
             * laravel自带的分页方法paginate()只支持传统web分页,对移动端的下拉分页不友好
             * 为了支持移动端,我们需要游标分页
             * 为了避免没处都手写游标分页,我们需要把游标分页进行封装,查看下一节
             */
            'data' => $query->page(),
        ]);
    }

    /**
     * http://domain/comment/sub/page
     */
    public function subpage(Request $request)
    {
        $post_id = $request->input('post_id', 3);
        $comment_id = $request->input('root_comment_id', 3);

        // 查询数据
        $query = SubComment::where('comment_id', $comment_id);

        // 返回结果
        return response()->json([
            'code' => 0,
            'data' => $query->page(),
        ]);
    }
}

游标分页

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {

        // 游标分页
        // 定义到任何一个使用中的Provider即可 比如App\Providers\AppServiceProvider
        Builder::macro('page', function ($current_cursor=0, $page_size=5) {
            $hasMoreQuery = clone $this;

            // 查询出结果
            $data = $this->when($current_cursor, function (Builder $query, string $current_cursor) {
                        $query->where('id', '>', $current_cursor);
                    })
                    ->limit($page_size)
                    ->get();

            // 下拉分页游标
            $next_cursor = $data->last() ? $data->last()->id : null ;

            // 判断是否还有下一页
            if(is_null($next_cursor)) {
                $has_more = false;
            }else {
                $has_more = $hasMoreQuery->where('id', '>', $next_cursor)->exists() ;
            }

            return [
                'current_cursor' => $current_cursor,
                'next_cursor' => $next_cursor,
                'has_more' => $has_more,
                'data' => $data,
            ];
        });
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
:) wink
唐章明
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 3

挺好的,可以在主表做父级id 然后省略第二章表么。

4个月前 评论
nff93

为啥不用 laravel 自带的游标分页?

另外,你这个分页有排序的情况就寄了

4个月前 评论

你这个也不是每一条评论查出一条二级评论啊, 这个写法应该是二级评论一共就查出一条, 再去和一级评论去匹配, 也就是说, 只有一条一级评论会查到一条二级评论(如果能匹配到的话)

file

3个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!