我有这样简单的关系
项目模型:
public function milestones()
{
return $this->hasMany('App\Models\Milestone');
}
里程碑模型:
public function milestones()
{
return $this->hasMany('App\Models\Milestone');
}
现在我尝试获取id = 3的Todo,它应该只在我的Todo所在的地方显示里程碑。这是我目前的解决方案(显示了项目中的所有里程碑)
$query = Project::whereHas('milestones',function($query) use ($UserId){
$query->whereHas('todo', function ($query) use ($UserId){
$query->where('id',3);
});
})->with('milestones.todo',function($query){
$query->where('id',3);
})
->get();
我如何将里程碑限制在那个里程碑中,id3的todo在哪里?
发布于 2021-09-08 03:11:08
您需要显式地使用您的急切加载:
$query = Project
::with('milestones', function ($query) use ($UserId) {
$query
->with([
'todos' => function ($query) use ($UserId) {
$query->where('id', $UserId);
},
])
->whereHas('todos', function ($query) use ($UserId) {
$query->where('id', $UserId);
});
})
->whereHas('milestones', function ($query) use ($UserId) {
$query->whereHas('todo', function ($query) use ($UserId) {
$query->where('id', $UserId);
});
})
->get()
https://stackoverflow.com/questions/69091056
复制