我有这个查询来列出所有用户及其对应关系。
用户有很多任务
每个任务有很多工作时间,这些工作时间可以有不同的用户
即,每个任务可以由不同的用户共享,因此他们对每个任务都有单独的工作时间。
我试过下面的代码
$users = User::select('users.id', 'users.first_name', 'users.last_name', 'users.contract_type')
->with([
'tasks' => function($query) use ($from, $to){
$query->whereBetween('date', [$from, $to])
->select('tasks.id', 'tasks.date');
},
'tasks.worktimes' => function($query) {
//$query->where('user_id', ?)
$query->withCount([
'tags as late_tag_count' => function ($subQuery) {
$subQuery->where('tags.id', 1);
},
'tags as early_tag_count' => function ($subQuery) {
$subQuery->where('tags.id', 2);
},
'tags as others_tag_count' => function ($subQuery) {
$subQuery->where('tags.id', 3);
}
]
);
}
])
->get();
这里的关系tasks.worktimes还获取其他用户的工作时间(这是预期的),但我想将其限制为仅获取父用户的工作时间。任何人请帮我弄清楚我应该使用什么其他条件来实现这一目标?
楷模
用户.php
public function tasks()
{
return $this->belongsToMany(Task::class, 'task_members')->withTimestamps();
}
任务.php
public function worktimes()
{
return $this->hasMany(Worktime::class, 'task_id');
}
工作时间.php
public function task()
{
return $this->belongsTo(Task::class);
}
public function users()
{
return $this->belongsToMany(User::class, 'task_members', 'task_id', 'user_id', 'task_id');
}