I have a request :
$comments = $post->comments()->with('replyComments:id,post_id,user_id,content,created_at,reply_comment_id')->orderBy('created_at', 'desc')->get();
When i try to echo this with a foreach, i have :
foreach ($comments as $comment) {
echo $comment;
};
Result :
{"id":383825,"post_id":553304,"user_id":6,"content":"test","created_at":"2022-04-28 15:16:19","reply_comment_id":null,"reply_comments":[{"id":383826,"post_id":553304,"user_id":6,"content":"reply test","created_at":"2022-04-28 16:20:16","reply_comment_id":383825}]}
But if i do :
foreach ($comments as $comment) {
echo $comment->replyComments; /* or $comment->reply_comments; */
};
The result is NULL
How could i echo reply_comments ?
Thanks for your help !
CodePudding user response:
reply_comments OR replyComments are not present in the output JSON. Hope you missed it on fetching!
$comments = $post->comments()->with('replyComments:replyComments')->orderBy('created_at', 'desc')->get();
This needs to add to the fetch code.
then
foreach ($comments as $comment) {
echo $comment->replyComments;
};
Or else you can use array.
displayData = [];
foreach ($comments as $comment) {
displayData[] = $comment->replyComments;
};
echo displayData;
CodePudding user response:
If you want to display the replies in one level like Youtube does, you need using the following querry:
$post = Post::with('comments.replyComments')->find($id);
$comments = $post->comments;
$replies = $comments->replyComments;
CodePudding user response:
use print_r($comment->replyComments)
instead of echo.