admin管理员组

文章数量:1404923

Hello i want to show the comments replies based on their parent id, for example you can play with data below.

CREATE TABLE `comments` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `slug` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
  `content` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `user_id` bigint NOT NULL,
  `comment_id` bigint NOT NULL DEFAULT '0',
  `depth_comment_id` bigint NOT NULL DEFAULT '0',
  `commentable_id` bigint NOT NULL,
  `commentable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `comments_slug_unique` (`slug`)
)
INSERT INTO `comments` (`id`, `slug`, `content`, `user_id`, `comment_id`, `depth_comment_id`, `commentable_id`, `commentable_type`, `created_at`, `updated_at`)
VALUES
    (1,'e074eb3a-b695-4c37-a063-37971084c6bd','test comment',1,0,0,1,'App\\Models\\Feed','2025-03-08 12:34:40','2025-03-08 12:34:40'),
    (2,'47891ac5-233f-4949-8e32-fda78543f61d','comment 2',1,0,0,1,'App\\Models\\Feed','2025-03-08 12:34:43','2025-03-08 12:34:43'),
    (3,'fe5f60c1-230e-407f-8b7b-f82fef40679a','abc',1,2,2,1,'App\\Models\\Feed','2025-03-08 12:35:08','2025-03-08 12:35:08'),
    (4,'0256e968-dfa9-444c-8a27-794d805e443e','abctest',1,2,2,1,'App\\Models\\Feed','2025-03-08 12:35:23','2025-03-08 12:35:23'),
    (5,'1608bc34-88ec-469c-8a1e-f32869525781','abctest child',1,3,2,1,'App\\Models\\Feed','2025-03-08 12:36:48','2025-03-08 12:36:48'),
    (6,'12edcdbd-5a2c-48be-b612-3e0ac29a9b1f','abctest child 2',1,4,2,1,'App\\Models\\Feed','2025-03-08 12:41:09','2025-03-08 12:41:09');

and sort based on parent like below.

this is if the comment sort what i expected

but i want to using Laravel Eloquent, i have try code like below, but i have no idea to make it what i expected.

public function replies(): CursorPaginator
    {
        $comments = Comment::with([
            'user', 
            'photos', 
            'depthComment', 
            'comment', 
            'commentCount'
        ]);
        
        if ($this->models && $this->callable) {
            $comments = $comments->whereHasMorph('commentable', $this->models, $this->callable);
        }

        $comments = $comments->whereHas('depthComment', function($query) {
            $query->where('slug', $this->slug);
        });

        $comments = $comments->orderBy('comment_id');
        $comments = $comments->latest();

        return $comments->cursorPaginate(
            config('app.front.pagination_limit')
        );
    }

Hello i want to show the comments replies based on their parent id, for example you can play with data below.

CREATE TABLE `comments` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `slug` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
  `content` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `user_id` bigint NOT NULL,
  `comment_id` bigint NOT NULL DEFAULT '0',
  `depth_comment_id` bigint NOT NULL DEFAULT '0',
  `commentable_id` bigint NOT NULL,
  `commentable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `comments_slug_unique` (`slug`)
)
INSERT INTO `comments` (`id`, `slug`, `content`, `user_id`, `comment_id`, `depth_comment_id`, `commentable_id`, `commentable_type`, `created_at`, `updated_at`)
VALUES
    (1,'e074eb3a-b695-4c37-a063-37971084c6bd','test comment',1,0,0,1,'App\\Models\\Feed','2025-03-08 12:34:40','2025-03-08 12:34:40'),
    (2,'47891ac5-233f-4949-8e32-fda78543f61d','comment 2',1,0,0,1,'App\\Models\\Feed','2025-03-08 12:34:43','2025-03-08 12:34:43'),
    (3,'fe5f60c1-230e-407f-8b7b-f82fef40679a','abc',1,2,2,1,'App\\Models\\Feed','2025-03-08 12:35:08','2025-03-08 12:35:08'),
    (4,'0256e968-dfa9-444c-8a27-794d805e443e','abctest',1,2,2,1,'App\\Models\\Feed','2025-03-08 12:35:23','2025-03-08 12:35:23'),
    (5,'1608bc34-88ec-469c-8a1e-f32869525781','abctest child',1,3,2,1,'App\\Models\\Feed','2025-03-08 12:36:48','2025-03-08 12:36:48'),
    (6,'12edcdbd-5a2c-48be-b612-3e0ac29a9b1f','abctest child 2',1,4,2,1,'App\\Models\\Feed','2025-03-08 12:41:09','2025-03-08 12:41:09');

and sort based on parent like below.

this is if the comment sort what i expected

but i want to using Laravel Eloquent, i have try code like below, but i have no idea to make it what i expected.

public function replies(): CursorPaginator
    {
        $comments = Comment::with([
            'user', 
            'photos', 
            'depthComment', 
            'comment', 
            'commentCount'
        ]);
        
        if ($this->models && $this->callable) {
            $comments = $comments->whereHasMorph('commentable', $this->models, $this->callable);
        }

        $comments = $comments->whereHas('depthComment', function($query) {
            $query->where('slug', $this->slug);
        });

        $comments = $comments->orderBy('comment_id');
        $comments = $comments->latest();

        return $comments->cursorPaginate(
            config('app.front.pagination_limit')
        );
    }
Share Improve this question asked Mar 9 at 3:39 Aris MunandarAris Munandar 277 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

ok i found the solution, hopefully this is the right way to solve my problem..

$comments = $comments->orderByRaw("
    (CASE WHEN (SELECT comment_id FROM comments AS parent WHERE parent.id = commentsment_id) = 0 THEN id ELSE comment_id END)
");
$comments = $comments->orderBy('id');
$comments = $comments->latest();

本文标签: laravelHow to sort comments replies based on their parent in EloquentStack Overflow