gpt4 book ai didi

php - Laravel 预加载 - 关系查询

转载 作者:行者123 更新时间:2023-12-02 01:04:14 25 4
gpt4 key购买 nike

假设我有这 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com