【发布时间】:2018-01-30 13:10:40
【问题描述】:
我有 用户、部门、职位、表格。
用户属于多个部门
部门有很多职位
用户属于多个职位
我想要一个查询,该查询将为单个部门提供用户列表及其在该部门中的职位。
$department = DepartmentView::with(['users.positions' => function($query) {
// do something?
}])->where('tag', $tag)
->get();
我也通过标签而不是 ID 获得部门。 职位表没有标签列,只有部门ID。
--
我可以通过定义静态 ID 使其工作,但问题是我通过标签获取部门。
public function getDepartmentByTag($tag) {
$departmentId = 1; FOR TESTING PURPOSE, IT WORKS
$department = DepartmentView::with(['users.positions' => function($query) use($departmentId) {
$query->whereHas('department', function($query) use($departmentId) {
$query->where('departmentId', $departmentId);
});
}])->where('tag', $tag)
->get();
}
【问题讨论】:
-
DepartmentView::with('users.positions' )->where('tag', $tag)->get()这应该可以解决问题 -
以前是这样,但它给出了用户在他所在部门的所有职位。
-
@JonathanLightbringer 在哪个表中有
tag列? -
如果您提供您在模型中定义的关系会更好
-
Alexey Mezenin,在部门表中。