Laravel 使用链式方法的复杂范围查询

Laravel Complex Scope Query using chain method

我想在第二种方法中使用 user id of whereHas 查询。我怎样才能做到这一点?

public function scopeloadAvailableAgents($query){
    return $query->whereHas('roles', function($q){
        $q->where("name", "agent");    
    })
    ->whereNotIn('id', function($q) use (/* i want to use user_id of where has query */){
        $q->select('user_id')
            ->from(with(new UserToManager())->getTable())
            ->where('user_id', $user_id);
    })
    ->get();
}

编辑: memeber 打错了。应该是 agent。我在 table agent_to_manager 上,我只想获取 agent_to_manager table 中不存在的代理角色用户,我有两个查询,其中一个只获取代理角色用户和第二次检查 agent_to_manager table 中不存在的用户,但我无法将 whereHas 查询的结果提供给 运行 第二个查询

  public function scopeloadAvailableAgents($query, $managerId){

        return $query->whereNotIn('id', function($q) use ($managerId){
            $q->select('user_id')
            ->from(with(new UserToManager())->getTable())
            ->where('manager_id', $managerId);
        })
        ->whereHas('roles', function($q){
            $q->where("name", "agent");
            
        })->get();
        
    }