假设我有这 3 个表 Blog、Post、Comment,其中有相应的模型 Blog、Post、Comment。
不,这里是他们之间的关系:
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表吗?
最佳答案
像这样查询嵌套关系:
$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 将相应地加载帖子,然后才会获取这些帖子的评论。
关于php - Laravel 预加载 - 关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383146/