JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "LoadChildComments.php"

Full Path: /home/markqprx/iniasli.pro/common-20260222054425/Comments/LoadChildComments.php
File size: 1.31 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace Common\Comments;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class LoadChildComments
{
    public function execute(
        Model $commentable,
        Collection $rootComments,
    ): Collection {
        $paths = $rootComments
            ->map(function (Comment $comment) {
                $path = $comment->getRawOriginal('path');
                return "LIKE '$path%'";
            })
            ->implode(' OR path ');

        $childComments = app(Comment::class)
            ->with(['user' => fn($builder) => $builder->compact()])
            ->where('commentable_id', $commentable->id)
            ->where('commentable_type', $commentable->getMorphClass())
            ->childrenOnly()
            ->where(fn(Builder $builder) => $builder->whereRaw("path $paths"))
            ->orderBy('path', 'asc')
            ->orderBy('created_at', 'desc')
            ->orderByWeightedScore()
            ->limit(100)
            ->get();

        $childComments->each(function ($child) use ($rootComments) {
            $index = $rootComments->search(
                fn($parent) => $parent['id'] === $child['parent_id'],
            );
            $rootComments->splice($index + 1, 0, [$child]);
        });

        return $rootComments;
    }
}