Rad*_*adi 5 php laravel eloquent laravel-5.2
是否可以在1级以上的深层次上与Eloquent进行多级关系查询?我的表看起来像这样:
post_comments-> id | comment | post_id | user_id | ...
post_comment_replies-> id | reply | post_comment_id | user_id | ...
users-> id | name | ....
user_data-> id |头像| ...
所以我想问的是有可能得到的评论的帖子的所有回复以及谁在1个查询与洋洋洒洒回复评论者的用户数据.
这就是我的评论模型的样子:
class PostComment extends Model{
public function replies(){
return $this->hasMany(PostCommentAwnsers::class);
}
public function user() {
return $this->belongsTo(User::class);
}
public function userInfo(){
return $this->belongsTo(UserInfo::class,'user_id','user_id');
}}
Run Code Online (Sandbox Code Playgroud)
public function index($id){
$posts = Post::find($id);
$postComments = PostComment::where('post_id','=',$id)->paginate(5);
return view('post.show',[
'post' => $post,
'postComments' =>$postComments
]);
}
Run Code Online (Sandbox Code Playgroud)
当我获得评论的所有用户数据时,我想获得回复者的所有用户数据.我真的很抱歉,如果这已经在其他地方被忽略或记录,但我似乎无法找到这个问题的确切解决方案.
你应该寻找急切的加载:https://laravel.com/docs/5.3/eloquent-relationships#eager-loading
如果你想获得所有帖子和他们的意见:
$posts = Post::with('comment')->get();
Run Code Online (Sandbox Code Playgroud)
如果您希望所有帖子都有评论和回复评论:
$posts = Post::with('comment.reply')->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2515 次 |
| 最近记录: |