0
나는 Laravel Eloquent에 약간의 문제가있어 도움이 필요합니다. 표시하고 싶습니다 1 답글 각 게시물의 1 답글. 여기 Laravel miltiple level relationship with()
내 테이블 입니다posts (id,title)
id | title
---------------------
1 | My post
---------------------
comments(id,post_id,comment,parent_id)
id | post_id | comment | parent_id
-----------------------------------------
1 | 1 | First comment | null
-----------------------------------------
2 | 1 | Second comment | null
-----------------------------------------
3 | null | 3rd comment | 1
-----------------------------------------
4 | null | 4th comment | 1
-----------------------------------------
5 | null | 5th comment | 2
-----------------------------------------
6 | null | 6th comment | 2
-----------------------------------------
내 모델 (웅변)
class Post extends Model
{
public function comments()
{
return $this->hasMany('Comment', 'post_id');
}
}
---------------------
class Comment extends Model
{
public function reply()
{
return $this->hasMany('Comment', 'parent_id');//self relationship
}
}
내 쿼리 기능
public function getPost($postId){
$posts = Post::with(['comment.reply'=>function($q){
$q->limit(1);
}])
->find($postId);
return $posts;
}
그리고 내가 결과를 얻을
{[
id=>1,
title=>'My post',
'comments'=>[
0=>[
id=>1,
comment=>'First comment',
parent_id=>null,
post_id=>1,
reply=>[
0=>[........(comment id:3).......]
]
],
1=>[
id=>2,
comment=>'Second comment',
parent_id=>null,
post_id=>1,
reply=>null
]
]
]}
는하지만이
{[
id=>1,
title=>'My post',
'comments'=>[
0=>[
id=>1,
comment=>'First comment',
parent_id=>null,
post_id=>1,
reply=>[
0=>[........(comment id:3,4)........]
]
],
1=>[
id=>2,
comment=>'Second comment',
parent_id=>null,
post_id=>1,
reply=>[
0=>[........(comment id: 5,6).........]
]
]
]
]}
이 친절하게 도와주세요처럼 싶어!
감사합니다. 노력은했지만 작동하지는 않습니다. :( –