我想搜索属于该公司的用户,并获得该公司在该公司中的位置
我的关系:
user
hasMany companies
company
hasMany departments
department
hasOne company
department
hasMany positions
position
hasOne department
user
有许多positions
(因为用户可以在许多公司中陈述)
我尝试这样做:
$slug = $request->company;
return User::where('name', 'like', $request->search . '%')
->whereHas('companies', function ($query) use ($slug) {
$query->whereSlug($slug);
})
->with('positions', function ($q) use ($slug) {
$q->whereHas('department.company', function ($que) use ($slug) {
$que->where('slug', $slug);
});
})
->get();