vli*_*o20 2 php orm laravel laravel-4
假设我有这 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()
Run Code Online (Sandbox Code Playgroud)
现在我想执行一些像这样的查询:
Blog::with(array('posts.comments' => function($q)
{
//query Post columns
})->find(1);
Run Code Online (Sandbox Code Playgroud)
据我所知,它$q对应于评论表。有没有办法查询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);
Run Code Online (Sandbox Code Playgroud)
Eloquent 会相应地加载帖子,只有这样它才会获取这些帖子的评论。
| 归档时间: |
|
| 查看次数: |
5117 次 |
| 最近记录: |