说我有这3个表博客,帖子,评论哪个有相应的模型博客,帖子,评论 这里没有关系:
Blog has many Post, posts()
Post belongs to Blog, blog()
Post has many Comment, comments()
Comment belongs to Post post()
现在我想执行一些这样的查询:
Blog::with(array('posts.comments' => function($q)
{
//query Post columns
})->find(1);
据我所知,$q
对应于Comment表。有没有办法查询Post表?
答案 0 :(得分:4)
查询嵌套关系,如下所示:
$blog = Blog::with(['posts' => function ($q) {
$q->where('column','value'); // query posts table
}, 'posts.comments' => function ($q) {
$q->where('commentsColumn','anotherValue'); // query comments table
}])->find(1);
Eloquent会相应地加载帖子,然后才能获取这些帖子的评论。