M
M
Maxim2020-04-23 16:07:28
Laravel
Maxim, 2020-04-23 16:07:28

How to display Laravel ready comments?

when loading comments with ajax

$('#moreComments').on('click', function () {
        $.post($(this).data('href'), function (data) {
            console.log(data);
        });
    });

I give comments in js from the controller like this

public function more(Request $request,$content,$type,$tale){
        if ($request->ajax()){
            $comArray = array();
            $moreComments = 0;
            $query = Comment::where('content_id', $content)->where('type', '=', $type)->where('parent','=',0)->orderBy('id', 'desc')->skip($tale);
            $comments = $query->take(2)->get();
            $countComments = $query->get()->count(); //Всего комментариев
            $getComments = 2; //По сколько подгружаем
            if ($countComments <= 2){ //Если количество оставшихся комменариев меньше либо равно общему количеству
                $moreComments = 0;
                $getComments = 0;
            }else{
                $moreComments = $countComments - 2; //Присваем переменно количество оставшихя комментариев
                if ($moreComments <= $getComments) $getComments = $moreComments; //Запись вида - показать $getChildrens из $moreChildrens
            }

            foreach ($comments as $comment) {
                $moreChildrens = 0; //Всего дочерних комментариев
                $getChildrens = 6; //По сколько подгружаем
                $queryChildrens = Comment::where('parent','=', $comment->id);
                $childrens = $queryChildrens->take(6)->get();
                $count = $queryChildrens->count();
                if ($count <= 6){ //Если количество оставшихся комменариев меньше либо равно общему количеству
                    $moreChildrens = 0;
                    $getChildrens = 0;
                }else{
                    $moreChildrens = $count - 6; //Присваем переменно количество оставшихя комментариев
                    if ($moreChildrens <= $getChildrens) $getChildrens = $moreChildrens; //Запись вида - показать $getChildrens из $moreChildrens
                }
                $comment->childrens = $childrens;
                $comment->more_childrens = $moreChildrens;
                $comment->get_childrens = $getChildrens;
            }

            return new CommentCollection($comments, $moreComments);
        }
    }

How can I return comments already with code? instead of records from basis?
now they are displayed like this,
5ea192b40d74a257047551.jpeg
but you need it like this
5ea192d3acaba280096103.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question