过滤条件查询

时间:2019-05-03 16:46:23

标签: laravel eloquent relationship

我想搜索属于该公司的用户,并获得该公司在该公司中的位置

我的关系:

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();

0 个答案:

没有答案