[英]Laravel eager loading - relationship query
说我有这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);
雄辩的将相应地加载帖子,然后才获取这些帖子的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.