Laravel - foreach 集合
Laravel - foreach on collection
我有一个请求:
$comments = $post->comments()->with('replyComments:id,post_id,user_id,content,created_at,reply_comment_id')->orderBy('created_at', 'desc')->get();
当我尝试用 foreach 回应这个时,我有:
foreach ($comments as $comment) {
echo $comment;
};
结果:
{"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}]}
但如果我这样做:
foreach ($comments as $comment) {
echo $comment->replyComments; /* or $comment->reply_comments; */
};
结果是NULL
我怎么回显 reply_comments?
感谢您的帮助!
编辑更多细节:
$comments = $post->comments()->with('likes','replyComments')->orderBy('created_at', 'asc')->get(['id','post_id','user_id','content','created_at','reply_comment_id']);
foreach($comments as $comment){
dd($comment);
}
$comments = $post->comments()->with('likes','replyComments')->orderBy('created_at', 'asc')->get(['id','post_id','user_id','content','created_at','reply_comment_id']);
foreach($comments as $comment){
dd($comment->likes);
}
$comments = $post->comments()->with('likes','replyComments')->orderBy('created_at', 'asc')->get(['id','post_id','user_id','content','created_at','reply_comment_id']);
foreach($comments as $comment){
dd($comment->replyComments);
}
关于点赞和回复评论,在Comment.php:
public function replyComments()
{
return $this->hasMany(self::class, 'reply_comment_id', 'id');
}
public function likes()
{
return $this->hasMany(\App\CommentLike::class, 'comment_id', 'id');
}
reply_comments 或 replyComments 不存在于输出 JSON 中。
希望您在获取时错过了它!
$comments = $post->comments()->with('replyComments:replyComments')->orderBy('created_at', 'desc')->get();
这需要添加到获取代码中。
然后
foreach ($comments as $comment) {
echo $comment->replyComments;
};
否则你可以使用数组。
displayData = [];
foreach ($comments as $comment) {
displayData[] = $comment->replyComments;
};
echo displayData;
如果你想像 Youtube 一样在一个级别显示回复,你需要使用以下查询:
$post = Post::with('comments.replyComments')->find($id);
$comments = $post->comments;
$replies = $comments->replyComments;
使用 print_r($comment->replyComments)
而不是 echo。
`$comment->replyComments` is an array and you should put it in foreach:
foreach($comment->replyComments as $item){
echo $item;
}
或写:
echo $comment->replyComments[0];
我有一个请求:
$comments = $post->comments()->with('replyComments:id,post_id,user_id,content,created_at,reply_comment_id')->orderBy('created_at', 'desc')->get();
当我尝试用 foreach 回应这个时,我有:
foreach ($comments as $comment) {
echo $comment;
};
结果:
{"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}]}
但如果我这样做:
foreach ($comments as $comment) {
echo $comment->replyComments; /* or $comment->reply_comments; */
};
结果是NULL
我怎么回显 reply_comments?
感谢您的帮助!
编辑更多细节:
$comments = $post->comments()->with('likes','replyComments')->orderBy('created_at', 'asc')->get(['id','post_id','user_id','content','created_at','reply_comment_id']);
foreach($comments as $comment){
dd($comment);
}
$comments = $post->comments()->with('likes','replyComments')->orderBy('created_at', 'asc')->get(['id','post_id','user_id','content','created_at','reply_comment_id']);
foreach($comments as $comment){
dd($comment->likes);
}
$comments = $post->comments()->with('likes','replyComments')->orderBy('created_at', 'asc')->get(['id','post_id','user_id','content','created_at','reply_comment_id']);
foreach($comments as $comment){
dd($comment->replyComments);
}
关于点赞和回复评论,在Comment.php:
public function replyComments()
{
return $this->hasMany(self::class, 'reply_comment_id', 'id');
}
public function likes()
{
return $this->hasMany(\App\CommentLike::class, 'comment_id', 'id');
}
reply_comments 或 replyComments 不存在于输出 JSON 中。 希望您在获取时错过了它!
$comments = $post->comments()->with('replyComments:replyComments')->orderBy('created_at', 'desc')->get();
这需要添加到获取代码中。
然后
foreach ($comments as $comment) {
echo $comment->replyComments;
};
否则你可以使用数组。
displayData = [];
foreach ($comments as $comment) {
displayData[] = $comment->replyComments;
};
echo displayData;
如果你想像 Youtube 一样在一个级别显示回复,你需要使用以下查询:
$post = Post::with('comments.replyComments')->find($id);
$comments = $post->comments;
$replies = $comments->replyComments;
使用 print_r($comment->replyComments)
而不是 echo。
`$comment->replyComments` is an array and you should put it in foreach:
foreach($comment->replyComments as $item){
echo $item;
}
或写:
echo $comment->replyComments[0];