How to load comments ajax in Laravel?

How to load comments via ajax?

send a request form the comments, but how to return the comments themselves, for example countComments field, the controller gave, like so

comments: {},
total,
...


public function more(Request $request,$content,$type,$tale){
 if ($request->ajax()){
 $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(); //Total comments

 foreach ($comments as $comment) {
 $moreChildrens = 0; //All child reviews
 $getChildrens = 6; //how many loadable
 $queryChildrens = Comment::where('parent','=', $comment->id);
 $childrens = $queryChildrens->take(6)->get();
 $count = $queryChildrens->count();
 if ($count <= 6){ //If the remaining number of comments is less than or equal to the total number
 $moreChildrens = 0;
 $getChildrens = 0;
}else{
 $moreChildrens = $count - 6; //variable Presuem the number of remaining review
 if ($moreChildrens <= $getChildrens) $getChildrens = $moreChildrens; //the asterisk is to show $getChildrens of $moreChildrens
}
 $comment->childrens = $childrens;
 $comment->more_childrens = $moreChildrens;
 $comment->get_childrens = $getChildrens;

}

}
 }
April 19th 20 at 12:30
1 answer
April 19th 20 at 12:32
return CommentResource::collection($comments);
total in the set, taken from here

Find more questions by tags Laravel